我是Obj-C和iOS开发的新手,并且已经设置了故事板和基本控制器。
我现在正试图在字典中对用户输入进行一些基本存储,但我并不是100%确定如何设置字典。我已经浏览了文档,但有点卡住了。
基本上,我想实现:
titleTextField
"访问输入值并将其设置为键title
我已粘贴在代码中,请参阅底部的功能,但如果我对所有属性的语法正确,我不能100%确定。您可以看到页面顶部定义的@property,然后我尝试访问底部addButtonTapped
函数中的那些。
非常感谢任何帮助或指导!
// AddViewController.m
// TodoApp
//
#import "AddViewController.h"
@interface AddViewController ()
@property (strong, nonatomic) IBOutlet UITextView *notesTextView;
@property (strong, nonatomic) IBOutlet UITextField *titleTextField;
- (IBAction)addButtonTapped:(UIBarButtonItem *)sender;
@end
@implementation AddViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)addButtonTapped:(UIBarButtonItem *)sender {
NSLog(@"Add button tapped");
//Create dictionary and store the values from the text fields and views
NSMutableDictionary *items = [NSMutableDictionary dictionaryWithObjectsAndKeys:_titleTextField,@"title", nil];
[items setObject:_titleTextField forKey:@"title"];
}
@end
答案 0 :(得分:2)
你走在正确的轨道上。这是你正在寻找的东西:
- (IBAction)addButtonTapped:(UIBarButtonItem *)sender {
NSLog(@"Add button tapped");
// Create dictionary and store the values from the text fields and views
// This is a class method, which you can tell because it is called
// with NSMutableDictionary as its receiver.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
// This is the modern way to set dictionary values. You could also
// use [dict setObject:self.titleTextField.text forKey:@"title] ,
// but that's outdated and less clear.
dict[@"title"] = self.titleTextField.text;
// Log out the value to confirm
NSLog(@"title : %@", dict[@"title"]);
}
根据我在您的代码中看到的误解,我建议阅读: