简体   繁体   中英

Loading ViewController via IBAction

I have an IBAction in my project, when user presses button, it should load a new view controller, however the code seems to be crashing

My code is as follows:

- (IBAction)PurchaseItem:(id)sender {
    PurchasedViewController *purchaseContr = (PurchasedViewController *)
        [self.storyboard instantiateViewControllerWithIdentifier:@"ShowAd"];

    _purchaseController.productID = @"com.id";

    purchaseContr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    [self presentViewController:purchaseContr animated:YES completion:NULL];
}

When I run the app on the device and press the button, the app crashed on the following line:

[self presentViewController:purchaseContr animated:YES completion:NULL];

The message I Have got is 'Thread 1: breakpoint 1.3'

My Crashlog is:

在此处输入图片说明

在此处输入图片说明

If you're looking for the right way to use Storyboards and Segues...

  1. In your storyboard, hold CTRL and click-drag from the originating view controller to the destination view controller. This creates and unnamed segue from A to B.
  2. Click on the line that was just created, and in the settings for your segue, change it to Modal (since that's the kind you appear to want in this case), and give your segue a descriptive name (you're using "ShowAD" in the code in the question, which is okay). You can also set the modal transitional style in interface builder, I believe.
  3. Make your button click action look like this

-(IBAction)purchasItem:(id)sender {
    [self performSegueWithIdentifier:@"ShowAD" sender:self];
}

Now, to set a property on the destination view controller, add another method to your originating view controller as such:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"ShowAD"]) {
        PurchasedViewController *purchaseContr = [segue 
            destinationViewController];
        purchaseContr.productID = @"com.id";
    }
}         

Now... with all this said, I think the problem actually is that you've manually set a break point. If you've got a little blue arrowish thing where the line numbers are, that's a manually added breakpoint. It stops your program execution at that line... but it's not a crash. You can right click the breakpoint and click "disable" or "delete" breakpoint, and it'll "fix" your "crash".

This isn't a crash, it looks like a manually set breakpoint! Disable all breakpoints in Xcode and run again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM