【问题标题】:Programmatically connect buttons using storyboard使用情节提要以编程方式连接按钮
【发布时间】:2012-01-03 20:54:59
【问题描述】:

几个小时以来,我一直在尝试解决我的应用程序中的一件大“事情”。我太累了,我什至不记得我应该用什么词代替“事物”这个词。 :p

这是我第一次在我的应用中使用故事板。

我以编程方式写出 X 数量的按钮,这些按钮稍后将取决于数据库中的数据。正如标题所说,我的问题是我正在使用情节提要,并且无法掌握如何在情节提要中创建按钮然后可以更改为的新视图。还有一些来自第一个视图的数据。

如果那可以帮助您帮助我,这里有一些代码。 ;p

- (void)viewDidLoad
{
    [super viewDidLoad];

    int heightGrowt = 0;

    for(int i = 0; i < 10; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        button.frame = CGRectMake(10, heightGrowt, 300, 40);

        heightGrowt = heightGrowt + 50;

        [button setTitle:@"First element" forState:UIControlStateNormal];

        [scrollWiew addSubview:button];
    }

    [scrollWiew setScrollEnabled:YES];
    [scrollWiew setContentSize:CGSizeMake(320, heightGrowt)];
}

如果您有/知道任何可以帮助我的事情,我将不胜感激。 :)

【问题讨论】:

  • 没有,scrollView也没有错。问题是我无法访问情节提要中的任何场景。自从我问起,我已经稍微更新了代码。我在 wiewDidLoad 中添加了这个: [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];这个函数: -(void)buttonPressed:(id)Sender 里面有这个: UIButton *button = (UIButton *)Sender; int id_ = button.tag; NSLog(@"标签值为:%d", id_);

标签: iphone xcode storyboard


【解决方案1】:
[theUIViewControllerThatYouAreSeguingFrom performSegueWithIdentifier:@"theIdentifier" sender:theViewCallingForTheSegue];

您可以在 Inspector 的 Attributes 部分中命名 segue。

【讨论】:

    【解决方案2】:

    好的,根据您的评论,您在正确的轨道上。

    为了从情节提要中检索它,您必须首先进入情节提要,选择要检索的视图控制器(单击视图控制器下方的带有横格纸的黄色圆圈 - 请注意,您必须放大才能看到它。)并给“标识符”字段(在属性检查器下列出)一个唯一的名称,您将使用它来引用这个特定的视图控制器。假设我们在本示例中使用 firstViewController

    在您的buttonPressed: 函数中,您需要从情节提要中获取所需的视图控制器,然后在按下正确的按钮时显示它。我假设当前视图是从情节提要中加载的(如果不是,您需要以不同的方式获取对情节提要的引用)。

     // I'm assuming that the class is UIViewController.  Change this if not.
     UIViewController *aViewController = [[self storyboard] instantiateViewControllerWithIdentifier:@"firstViewController"];  
    
     // Once you have retrieved it, you display it like you would any other view controller.  
     // I.e. if you want to push it onto a UINavigationController stack you would use:  
     if (aViewController != nil) {  
       [self.navigationController pushViewController:aViewController animated:YES];  
     }
    

    【讨论】:

    • 哇,谢谢!这解决了这一切:) 我正要放弃,但现在我可以继续了!非常感谢!
    • 不客气!请接受这是正确的答案,因为它解决了您的问题!
    【解决方案3】:

    每个故事板视图都有一个 UIViewController 类集,就像您为单独的 XIB 所做的那样 - 在您的项目中创建一个新的 UIViewController 子类文件,并将其设置为 IB Utilities Inspector 中的“自定义类 - 类”属性故事板中的适当视图。

    然后,您应该能够在需要时从您的按钮实例化此 UIViewController 对象,并将它们推送到 NavigationController 堆栈上,或者根据需要进行转换。

    查看这些链接以获得非常好的故事板教程:

    http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1

    http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多