UiButton / IBAction - link from a RootView to main

2019-04-02 12:34发布

问题:

I try to call the main ViewController on my storyboard. In my app there is a additional .h, .m file with no xib or storyboard.

In this .m file T craeted a button:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(home:)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
NSLog(@"Home-Button line 645");

This button should link to my main ViewController in the Storyboard. The view has the identifier HauptMenu. I got no error, but the view doesnt change to my main ViewController. What is wrong?

    - (IBAction)home:(id)sender {
    NSLog(@"Button was tapped");
    ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

    NSLog(@"1");
    [viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    NSLog(@"2");
    [self.navigationController pushViewController:viewController animated:NO];
    [viewController release];
    NSLog(@"3");

}

回答1:

If your .m file is not associated with any storyboard, wouldn't self.storyboard be Nil?

Try:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                               @"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

Make sure to change the storyboardWithName: to whatever your storyboard is named.


You may not have gotten any errors because Objective-C handles nil differently than other languages, it (usually) won't throw an exception if you try to call a method on nil, it will just return nil. The following code will happily run, throwing no compiler or runtime errors:

UIViewController * test = nil;
[test viewDidLoad];

NSString * storyBoardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyBoardName = @"MainStoryboard_iPad";
} else {
    storyBoardName = @"MainStoryboard_iPhone";
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   storyBoardName bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];


回答2:

I had this problem myself. Thanks to this answer now it works!!

Here is my code, hopefully someone else can use this:

 // Declare the storyboard
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

 // Declare the next view controller
 NextViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];

 // Pass value
 nextViewController.result = result;
 [currentObject memberfunction:value];

 // Define transition
 nextViewController.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

 // Perform change
 [self presentModalViewController:nextViewController animated:YES];