Objective c 如何在objective c的表格视图中单击按钮重定向到新屏幕

Objective c 如何在objective c的表格视图中单击按钮重定向到新屏幕,objective-c,uitableview,uibutton,Objective C,Uitableview,Uibutton,我已经创建了一个表视图,添加了一些标签并显示了它,现在我需要在每个单元格中都有一个按钮,单击按钮,它就会重定向到新屏幕。 下面是我试图重定向的代码 - (void)viewDidLoad { vendorsArray = [CommonUserDetails sharedUserDetails].mVendorDictionary; NSLog(@"vendorsArray is %@ \n count is %d",vendorsArray,[vendorsArray coun

我已经创建了一个表视图,添加了一些标签并显示了它,现在我需要在每个单元格中都有一个按钮,单击按钮,它就会重定向到新屏幕。 下面是我试图重定向的代码

- (void)viewDidLoad
{
    vendorsArray = [CommonUserDetails sharedUserDetails].mVendorDictionary;
    NSLog(@"vendorsArray is %@ \n count is %d",vendorsArray,[vendorsArray count]);

    loTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 43, 320, 400) style:UITableViewStylePlain];
    [loTableView setBackgroundColor:[UIColor whiteColor]];

    loTableView.delegate = self;
    loTableView.dataSource = self;
    loTableView.separatorColor = [UIColor grayColor];
    [self.view addSubview:loTableView];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (cell == nil) 
    {
        cell = [self tableviewCellWithReuseIdentifier:@"myCell"];

    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    [self configureCell:cell forIndexPath:indexPath];
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

#define L1_TAG 1
#define L2_TAG 2
#define L3_TAG 3
#define L4_TAG 4

- (UITableViewCell *)tableviewCellWithReuseIdentifier:(NSString *)identifier 
{
    UITableViewCell* cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 320, 100)reuseIdentifier:identifier];

    UILabel* l1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 290, 30)];
    [l1 setBackgroundColor:[UIColor clearColor]];
    [l1 setFont:[UIFont systemFontOfSize:14]];
    // [l1 setTextColor:[UIColor blackColor]];
    l1.textColor = [UIColor colorWithRed:153/(CGFloat)255 green:51/(CGFloat)255 blue:0 alpha:1.0];
    [l1 setAdjustsFontSizeToFitWidth:YES];
    [l1 setTextAlignment:UITextAlignmentRight];
    l1.tag = L1_TAG;
    [cell.contentView addSubview:l1];


    UILabel* l2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 20 , 300, 30)];
    [l2 setBackgroundColor:[UIColor clearColor]];
    l2.font = [UIFont boldSystemFontOfSize:14];
    l2.textColor = [UIColor blackColor];
    //l2.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:153/(CGFloat)255 alpha:1.0];
    l2.tag = L2_TAG;
    [l2 setTextAlignment:UITextAlignmentLeft];
    [cell.contentView addSubview:l2];

    UILabel* l3 = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, 190, 30)];
    [l3 setBackgroundColor:[UIColor clearColor]];
    [l3 setFont:[UIFont systemFontOfSize:14]];
    [l3 setTextColor:[UIColor grayColor]];
    // l3.textColor = [UIColor colorWithRed:102/(CGFloat)255 green:102/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
    [l3 setAdjustsFontSizeToFitWidth:YES];
    l3.tag = L3_TAG;
    [cell.contentView addSubview:l3];

    UILabel* l4 = [[UILabel alloc] initWithFrame:CGRectMake(5, 60, 190, 30)];
    [l4 setBackgroundColor:[UIColor clearColor]];
    [l4 setFont:[UIFont systemFontOfSize:14]];
    [l4 setTextColor:[UIColor grayColor]];
    //l4.textColor = [UIColor colorWithRed:0/(CGFloat)255 green:51/(CGFloat)255 blue:0/(CGFloat)255 alpha:1.0];
    [l4 setAdjustsFontSizeToFitWidth:YES];
    l4.tag = L4_TAG;
    [cell.contentView addSubview:l4];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(150, 60, 70, 30);
        [button setTitle:@"Pay" forState:UIControlStateNormal];
        [cell.contentView addSubview:button];
    [button addTarget:self action:@selector(payButton)
          forControlEvents:UIControlEventTouchUpInside];


    return cell;
}

- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath 
{

    UILabel *myl1 = (UILabel*)[cell viewWithTag:L1_TAG];
    UILabel *myl2 = (UILabel*)[cell viewWithTag:L2_TAG];    
    UILabel *myl3 = (UILabel*)[cell viewWithTag:L3_TAG];
    UILabel *myl4 = (UILabel*)[cell viewWithTag:L4_TAG];

    NSDictionary *dictionary = [vendorsArray objectAtIndex:indexPath.row];
    myl1.text= [NSString stringWithFormat:@"INo:%@",[dictionary objectForKey:@"iNo"]];
    myl2.text=[dictionary objectForKey:@"vendorName"];
    myl3.text= [NSString stringWithFormat:@"Amount:%@",[dictionary objectForKey:@"amount"]];
    myl4.text= [NSString stringWithFormat:@"DDate:%@",[dictionary objectForKey:@"dDate"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"count is %d",[vendorsArray count]);
    return [vendorsArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{   
     return @"Payables";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100; 
}
-(IBAction) payButton{

    PayDescriptionController *mPayDescriptionController = [[PayDescriptionController alloc]initWithNibName:@"PayDescriptionController" bundle:nil];
    mPayDescriptionController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.navigationController presentModalViewController:mPayDescriptionController animated:YES];
}
上面代码中出现错误的地方。

试试这个

[self.navigationController pushViewController:mPayDescriptionController animated:YES];

看,它是否正在导航到下一个视图!!如果没有,则可能是您的一个对象为空。

您的代码出现了什么错误?我正在调用按钮中的Pay按钮,该按钮位于表视图中,单击按钮后,它将重定向到新屏幕,即payDescriptionController,但点击按钮时它不会显示该页面。您确定它正在调用
payButton
?您是用NSLog检查的还是断点检查的?是的,我用NSLog检查过,它是在控制台中点击按钮打印的。您的nib是否分配了支付描述控制器