objective-c - 单击 UIButton 以编程方式创建 Segue 或推送到另一个 View Controller

标签 objective-c uitableview ios5 pushviewcontroller segue

我现在正在尝试解决这个问题有一段时间了。我正在 TableView 单元格中以编程方式创建 UIButton。单击按钮后,我想继续或推送到另一个 View Controller 。如果我使用 StoryBoard,在其中创建按钮,然后按住 Control 键并将 Segue 拖动到目标 VC,那么这就是小菜一碟。然而,出于各种原因,我不想这样做。以下是迄今为止我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath



// <<<< bunch of code snipped out here to keep this example short >>>



for(UIButton *button in cell.subviews)
{
    [button removeFromSuperview];
}
UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
[imageButton setImage:image forState:UIControlStateNormal];
imageButton.layer.masksToBounds = YES;
imageButton.layer.cornerRadius = 5.0;
imageButton.layer.borderWidth = 1.0;
imageButton.layer.borderColor = [[UIColor grayColor] CGColor];

CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
imageButton.transform = rotateButton;

cell.backgroundColor = [UIColor clearColor];
[cell addSubview:imageButton];

return cell;

现在,将以编程方式为此表中的每一行创建一个带有图像的按钮。我还旋转按钮 - 是的,这是一个水平表格 View 。

那么我该如何对按钮点击采取行动呢?所有按钮都将转到相同的目标 View Controller ...

PS - 我正在使用带有 ARC 的 Xcode 4.2(不确定这是否真的重要)。


-----问题更新

我已经实现了代码建议(感谢 jrturton),但是我相信,因为我位于 UITableView 单元格中,所以我无法呈现模态视图 Controller 。我现在有以下代码...

.h 文件:

@interface DetailsHorizontalPhotoCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) IBOutlet UITableView *horizontalTableView;
@property (nonatomic, strong) NSArray *contentArray;

- (IBAction)photoButton:(id)sender;
@end

.m 文件:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    /// ----- code here to setup the cell, button, image, etc...

    // do something when the buttong is pressed
    [imageButton addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (IBAction)photoButton:(id)sender 
{
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
    NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];

    // do some stuff here to pull object data out of the contentArray
    NSLog(@"Image clicked, index: %d", hitIndex.row);
    // other non-relevant code snipped to keep this example short

    // next I want to present a modal view controller (lets call it PhotoAlbum) which is a TableViewController

    PhotoAlbum *photoAlbum = [[PhotoAlbum alloc] init];

    // below does not work, does not even autocomplete in Xcode. I believe its because I'm in a UITableViewCell and not a VC... Any suggestions here?
    [photoAlbum setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:photoAlbum animated:YES];
}

最佳答案

我不确定为什么您每次都在单元格中重新创建按钮而不是更改图像,但现在让我们忽略它。

您可以将所有按钮连接到 TableView Controller 中的同一操作,该操作可以像往常一样采用 sender 参数。

您添加如下操作:

[imageButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

这会调用 View Controller 中的方法,其签名如下:

 -(void)buttonPressed:(UIButton * sender)

在此方法中,您可以检测哪一行被单击,如下所示:

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];

然后执行您喜欢的任何其他操作。

关于objective-c - 单击 UIButton 以编程方式创建 Segue 或推送到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9205212/

相关文章:

iOS 在 MFMailComposeViewController 和 SKStoreProductViewController 的 init 方法上崩溃

ios - UITableViewAutomaticDimension 不调整到 UILabel 文本高度

objective-c - 将 Storyboard 更新到 iOS 6 并向后兼容

ios - 如何在没有 Storyboard的情况下调整 iPad 和 iPhone 4S 的 View 大小?

iphone - 如何将 double 转换为 NSInteger?

ios - 重新加载 TableView 时获取索引错误

iphone - UIWebView加载本 map 片的时滞

objective-c - `[blah addObject:@"1"];` 和 `[blah insertObject:@"0"atIndex:0];` 有什么区别?

ios - 使用点击手势时如何获取 UICollectionView header 的索引路径?

ios - UIScrollView 后面的 UITableView