简体   繁体   中英

Present view from delegate modally in iOS 5

I cannot seem to find this anywhere online. I have an add button in one of my views and I have hooked it up to an IBAction method called add . In my storyboard, I have created a view that has a form all set up on it. I have assigned a class to that view in the storyboard as well. That class is called AddItemViewController .

I am trying to present this view modally and then set the delegate to the view that called the AddItemViewController. However, all I get is an empty UITableViewController that shows up. Here is my code that I'm trying to use:

- (IBAction)add {
    AddItemViewController *addItem = [[AddItemViewController alloc] init];
    addItem.delegate = self;
    [self presentModalViewController:addItem animated:YES];
}

Is there anything I'm missing? Why does it just show an empty table and not the view controller that I set up in the storyboard?

Here is the code from the AddItemViewController:

@interface AddItemViewController : UITableViewController <UITextFieldDelegate> {
}

@property (strong, nonatomic) IBOutlet UITextField *note;

- (void)save:(id)sender;
- (void)cancel:(id)sender;
@end


@implementation AddItemViewController
    - (void)viewDidLoad {

    }

    - (IBAction)cancel:(id)sender {
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    - (IBAction)save:(id)sender {
        DbHandler *db = [[DbHandler alloc] init];
        [db executeUpdate:self.note];

        [self dismissViewControllerAnimated:YES completion:nil];
    }
@end

Well, AddItemViewController inherits from UITableViewController, not UIViewController, so it makes sense that a UITableViewController is showing up.

You should initiate the AddItemViewController like this:

AddItemViewController *addItem = [[AddItemViewController alloc] initWithNibName:@"AddItemViewController"];

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