ios - 以编程方式添加按钮以执行 segue

标签 ios iphone objective-c uibutton segue

我正在尝试使用代码“即时”创建一个按钮,并且我希望该按钮能够执行
转到不同的 Controller 。据我所知,以编程方式创建 segue 是不可能的,但是如果创建的按钮不在我的 Storyboard上,我怎么能通过呢?

这是创建按钮的代码:

- (void)addButton:(UIView*)view inLocation:(CGPoint)point
{

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    but.frame= CGRectMake(point.x, point.y,30,30);
    [but setTitle:@"CHOOSE" forState:UIControlStateNormal];
    // The nilSymbol should call a method that will perform the segue
    [but addTarget:self action:@selector(nilSymbol) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but];  
}

有什么选择吗?

最佳答案

在 Interface Builder 中,创建一个 segue。
由于您无法将 segue 连接到按钮(它还不存在),只需将 segue 连接到起始 ViewController。
然后,当您创建按钮时:

// ....
[but addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// ....

在 someMethod 中:
-(void)someMethod
{
    [self performSegueWithIdentifier:@"segueIdentifier" sender:self];
}

关于ios - 以编程方式添加按钮以执行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241194/

相关文章:

iOS 在属性变量声明中覆盖自定义 getter/setter

使用四元数的 iPhone GLGravity 示例

c++ - Objective-C 错误 : C style method inside Class implementation?

objective-c - 在 Objective C 中按顺序保持 JSON 对象的字典键

ios - 应用程序安装失败 : App has disallowed Info. plist key

objective-c - 在 Objective-C 中更改预处理器值

ios - 使用平移、旋转和捏合手势编辑后如何保存 UIImage

ios - 如何在同一个 View Controller 中使用 2 个 UIPickerViews?

ios - 无法更改共享扩展导航栏标题颜色(Swift)

iphone - UITableView 类似于 gridView