【问题标题】:use prepareForSegue for button使用 prepareForSegue 作为按钮
【发布时间】:2012-06-21 09:15:42
【问题描述】:

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

提前致谢!

这是我的代码

创建按钮

 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];

按钮操作

-(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];


}

准备继续

- (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
    }}

错误

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

直接从视图控制器启动的 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"];
    }}

【问题讨论】:

标签: objective-c ios xcode storyboard


【解决方案1】:

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

  1. 老办法,全在代码中。这就是你所做的。此时您可以完全忘记转场。

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

.

[self performSegueWithIdentifier:myIdentifyer sender:self];

其中 self 是视图控制器。现在您可以在prepareForSegue: 中进行自定义。

【讨论】:

    【解决方案2】:

    本例中的示例代码只是告诉您可以在prepareForSegue:sender: 中执行的操作类型。

    这表明您尝试连接的 viewController 将有一个方法 setMyObjectHere: 并且它会期望您传递该对象。

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

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

    【讨论】:

    • 添加了一张图片,告诉你在哪里添加 id 以摆脱警告。
    【解决方案3】:

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

    在应用程序委托中创建一个根视图控件,而不仅仅是弹出和关闭下一个视图控制器等。

    例如,这就是您在视图控制器中所需要的:

    -(void)nextControllerButton:(UIButton*)button{
    
        SearchViewController *nextController = [[SearchViewController alloc] init];
    
        nextController.username = _username.text;
        nextController.password = _password.text;
    
        [self.navigationController showViewController:nextController sender:self];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多