Open Storyboard-view manually

人盡茶涼 提交于 2019-12-04 22:03:08

问题


I have an application using Storyboard. On a view there is an AlertViewDialog.

When the user clicks the first button ("Yes"), how can I open an other view on the Storyboard?


回答1:


my be this can help :

  1. Drag a View in Then go to Identity Inspector (Shortcut: option+apple+3).
  2. Select the newly dragged View and give unique name from identify inspector in title Storyboard ID . // see the image for reference

create SecondViewController class (.h &.m) subclass of viewController .

then from alert view code (as you said when YES is clicked )

paste the code mentioned below

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

let me know if any issues occur.




回答2:


May this helps:

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



回答3:


The first thing you need to do is set the UIAlertView delegate to do this add UIAlertViewDelegate to your @interface so it looks like

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

and in the @implementation you can then added something like

      @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

Remember to set the unique identifier in the storyboard. You can do this by going to your .storyboard and in the Identity Inspector (Selecting the third one in) you can set the Storyboard ID this is what you will need to match with what is in instantiateViewControllerWithIdentifier so in the case above it would be "secondViewController". It is that simple.

Remember to dismiss this view when you are done you will need to use

       [self dismissModalViewControllerAnimated:YES]; 

The above has actually been deprecated in iOS 6.0 but you can use

       [self dismissModalViewControllerAnimated:YES completion:nil];

Which does the same thing except it adds a completion block on the end.

Hope this helps.



来源:https://stackoverflow.com/questions/14381418/open-storyboard-view-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!