ios 对按钮使用 prepareForSegue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11135047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:52:55  来源:igfitidea点击:

use prepareForSegue for button

objective-ciosxcodestoryboard

提问by john.k.doe

Hi I create button programmatically and I connect my button to another view, but I got segue problem that I should use prepareForSegue method for storyboard but I don't know how there ar esome sample to the internet but I will get error when I used that sample, would you please help me

嗨,我以编程方式创建按钮并将我的按钮连接到另一个视图,但是我遇到了 segue 问题,我应该将 prepareForSegue 方法用于故事板,但我不知道互联网上如何有一些示例,但是当我使用它时会出错样品,你能帮我吗

Thanks in advance!

提前致谢!

here is my code

这是我的代码

Creating Button

创建按钮

 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
        button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
        button.tag = currentTag;
        currentTag++;
        [button.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [button.layer setBorderWidth: 1.0];
        [button setTitle:[NSString stringWithFormat:@"%d",currentTag] forState:UIControlStateNormal];
        button.frame = CGRectMake(80*x, 32*y, 80, 32); 
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonView addSubview: button];

Action for button

按钮操作

-(void)buttonPressed:(UIButton *)button
{
NSLog(@"button %u -- frame: %@", button.tag, NSStringFromCGRect(button.frame));
WeekView *crtObj=[[WeekView alloc]initWithNibName:@"WeekView" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];


}

Prepare for segue

准备转场

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"WeekView"]) {

        // Get reference to the destination view controller
       WeekView *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        [vc setMyObjectHere:object];  //// I don't know what should I write instead of object
    }}

error

错误

enter image description here

在此处输入图片说明

Edit 2:**** I have a warning :

编辑 2:**** 我有一个警告:

Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]

直接从视图控制器启动的 Segue 必须有一个标识符用于 -[UIViewController performSegueWithIdentifier:sender:]

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"WeekView"]) {

        // Get reference to the destination view controller
     //  WeekView *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
       // [vc setMyObjectHere:object];
    [segue.destinationViewController setTitle:@"WeekView"];
    }}

回答by Mundi

There are really two ways to push a view controller onto the navigation controller.

有两种方法可以将视图控制器推送到导航控制器上。

  1. The old way, all in code. That is what you did. You can completely forget about segues at this point.

  2. The new way: establish a Segue in Storyboard (push segue from one view controller to a new view controller, which you also define in Storyboard), give it an Identifiername and then use this code to push the view controller.

  1. 旧的方式,全部在代码中。那就是你所做的。此时您可以完全忘记 segue。

  2. 新方法:在 Storyboard 中建立一个 Segue(将 segue 从一个视图控制器推送到一个新的视图控制器,您也在 Storyboard 中定义),给它一个Identifier名称,然后使用此代码推送视图控制器。

.

.

[self performSegueWithIdentifier:myIdentifyer sender:self];

where self is the view controller. Now you can do the customizations in prepareForSegue:.

其中 self 是视图控制器。现在您可以在prepareForSegue:.

回答by john.k.doe

the sample code in this case is just telling you the type of thing you can do in prepareForSegue:sender:.

在这种情况下,示例代码只是告诉您可以执行的操作类型prepareForSegue:sender:

it is suggesting that the viewController to which you are attempting to segue would have a method setMyObjectHere:and it would be expecting you to pass that object.

这表明您试图转入的 viewController 将有一个方法,setMyObjectHere:并且它希望您传递该对象。

as an example, your class might have a setTitle:method, and in the prepareForSegue:sender:code, you could pre-set the title based on information in the controller you are performing the segue from.

例如,您的类可能有一个setTitle:方法,在prepareForSegue:sender:代码中,您可以根据正在执行转场的控制器中的信息预先设置标题。

to get rid of the warning, you need an identifier, as in the picture below:

要消除警告,您需要一个标识符,如下图所示:

enter image description here

在此处输入图片说明

回答by Travis Delly

"The old way to code", no the better way to code hopefully this helps, but if your using UIButtons and UIViews programmatically you shouldn't even be using segues.

“旧的编码方式”,没有更好的编码方式希望这会有所帮助,但是如果您以编程方式使用 UIButtons 和 UIViews,您甚至不应该使用 segues。

Create a root view control in app delegate, than just pop on and off the next viewcontrollers and such.

在创建应用程序委托一个根查看控制,比只是弹出和关闭下一viewcontrollers和这样。

For example this is all you need in your viewcontrollers:

例如,这就是您在视图控制器中所需的全部内容:

-(void)nextControllerButton:(UIButton*)button{

    SearchViewController *nextController = [[SearchViewController alloc] init];

    nextController.username = _username.text;
    nextController.password = _password.text;

    [self.navigationController showViewController:nextController sender:self];
}