iOS 5 中委托模式呈现视图
我似乎无法在网上找到这个。我的一个视图中有一个添加按钮,并且我已将其连接到名为 add
的 IBAction
方法。在我的故事板中,我创建了一个视图,其中设置了所有表单。我也在故事板中为该视图分配了一个类。该类称为 AddItemViewController
。
我试图以模态方式呈现此视图,然后将委托设置为调用 AddItemViewController 的视图。然而,我得到的只是显示一个空的 UITableViewController 。这是我尝试使用的代码:
- (IBAction)add {
AddItemViewController *addItem = [[AddItemViewController alloc] init];
addItem.delegate = self;
[self presentModalViewController:addItem animated:YES];
}
我缺少什么吗?为什么它只显示一个空表,而不显示我在情节提要中设置的视图控制器?
以下是 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
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
好吧,AddItemViewController 继承自 UITableViewController,而不是 UIViewController,因此显示 UITableViewController 是有意义的。
您应该像这样启动
AddItemViewController
:AddItemViewController *addItem = [[AddItemViewController alloc] initWithNibName:@"AddItemViewController"];
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"];