如何基于分段控件和添加按钮转换为2个视图?

时间:2012-01-04 15:03:07

标签: iphone objective-c ios storyboard uisegmentedcontrol

我在导航控制器的标题中有一个分段控件,我想将一个对象添加到也在此导航控制器中的表视图控制器中。

继承问我的问题:

如何从一个[+] UIButton切换到2个视图中的一个,每个分段控件选项的不同添加视图。你只能从故事板中的UIBarButtonItem连接一个segue,但我不确定如果我只挂了一个然后在某处更改代码或者如果我必须构建单独的xib文件(我不熟悉,我是iPhone开发人员的新手,或者这是如何工作的!

请帮忙!

2 个答案:

答案 0 :(得分:10)

你做了我称之为“通用”的segue,它与动作/触发器无关。请在此处查看我的回答:How to make and use generic segue

制作其中2个segue,然后在您的IBAction方法中进行segmentedControl调用performSegueWithIdentifier:。例如:

- (IBAction)segmentCtrlChanged:(id)sender {
 UISegmentedControl *seg = sender;
 if (seg.selectedSegmentIndex == 0) 
   [self performSegueWithIdentifier:@"segue1" sender:self];
 else if (seg.selectedSegmentIndex == 1) 
   [self performSegueWithIdentifier:@"segue2" sender:self];
}

答案 1 :(得分:1)

您的故事板中只需要一个segue。 在你的viewcontroller中,添加

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

方法,并使用switch来决定选择哪个细分。

e.g。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    switch (self.segmentedButton.selectedSegmentIndex)
    {
          case 0:
          {
                UIView1 *view1 = (UIView1 *)segue.destinationViewController;
                // do other customization if needed
                break;
          }
          case 1:
          {
                UIView2 *view2 = (UIView2 *)segue.destinationViewController;
                // do other customization if needed
                break;
          }
          default:
               break;
    }
}

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIStoryboardSegue_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UIStoryboardSegue

相关问题