手动打开故事板视图

6

我有一个使用Storyboard的应用程序。在一个视图上有一个AlertViewDialog。

当用户点击第一个按钮(“是”)时,我如何在Storyboard上打开另一个视图?

3个回答

10

也许这可以帮助:

  1. 拖动一个视图,然后进入“Identity Inspector”(快捷键:option+apple+3)。
  2. 选择新拖动的视图,并在“Identity Inspector”中为其设置唯一名称,即标题Storyboard ID。 // 参考下面的图片 enter image description here

创建SecondViewController类(.h和.m),它是viewController的子类。

然后从警报视图代码中(正如您所说的当YES被点击时),粘贴以下代码:

SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"vinay"];
        [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
        [self presentViewController:svc animated:YES completion:nil];

如果出现任何问题,请告诉我。


5

希望这能有所帮助:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ClassNameViewController *viewController = (ClassNameViewController *)[storyboard instantiateViewControllerWithIdentifier:@"viewIdentifierOnStoryboard"];
[self presentModalViewController:viewController animated:NO];

你是否在视图上设置了标识符并使用了相同的故事板名称? - Tchelow
我已将Storyboard ID设置为“eerstekeer”。勾选了“使用Storyboard ID”,导入startup.h(类名)并使用以下代码:UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; startup *viewController = (startup *)[storyboard instantiateViewControllerWithIdentifier:@"eerstekeer"]; - Rick de Jong
有一行代码我忘了……那就是呈现视图的那一行。没有它,什么都不会发生。请添加这一行并再次尝试。 - Tchelow

0

你需要做的第一件事是设置 UIAlertView 的代理。为此,请将 UIAlertViewDelegate 添加到你的 @interface 中,使其看起来像这样:

      @interface myClass : super <UIAlertViewDelegate>
            // super could be anything like `UIViewController`, etc
      @end

然后在@implementation中,你可以添加类似以下的内容

      @implementation myClass

      ........... Some code


      - (IBAction)someActionMethod:(id)sender
      {
            UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:nil
                                                                  message:@"Would you like to move on?"
                                                                 delegate:self
                                                        cancelButtonTitle:@"No"
                                                        otherButtonTitles:@"Yes", nil];
             [myAlertView show];
             // [myAlertView release]; Only if you aren't using ARC
      }

      - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
      {
              switch(buttonIndex) {
                    case 1:
                          SecondViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
                          [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
         //  [self presentViewController:svc animated:YES]; // Deprecated in iOS 6.0
                          [self presentViewController:svc animated:YES completion:nil]; // Introduced in iOS 5.0
                          break;
                    default:
                          break;
              }
      }

      @end

记得在Storyboard中设置唯一标识符。您可以通过进入您的.storyboard并在Identity Inspector(选择第三个)中设置Storyboard ID来完成此操作,这是您需要与instantiateViewControllerWithIdentifier中的内容匹配的内容,因此在上面的情况下,它将是"secondViewController"。就是这么简单。

完成后,请记得关闭此视图,您需要使用

       [self dismissModalViewControllerAnimated:YES]; 

上述方法在iOS 6.0中已被弃用,但您可以使用

       [self dismissModalViewControllerAnimated:YES completion:nil];

这个做的事情和之前一样,只不过在末尾添加了一个完成块。

希望能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接