我试图建立一个餐厅排队应用,其第一页将有三个按钮,其中一个是“新预订”。点击它时,它会打开一个新视图,其中包含客户姓名,电话号码和总人数,服务员将输入他们订单的预计时间,然后按Enter键将所有这些值保存在plist中。我指的是youtube视频来创建plist,但是我有一个视图控制器类,我在其中初始化plist以及一个控制保存数据的NewReservation类。根据视频,我在viewDidLoad方法中创建plist,并在按钮的IBAction中复制几行,然后输入详细信息"。我弄错了,无法访问NewReservation课程中的值,请澄清一下。 谢谢。
ViewController.h
#import <UIKit/UIKit.h>
@class NewReservation;
@interface ViewController : UIViewController
@property (nonatomic , strong) NewReservation *Res;
@end
ViewController.m
#import "ViewController.h"
#import "NewReservation.h"
@interface ViewController ()
@end
@implementation ViewController{
NSMutableArray *phoneNumbers , *name , *noOfPeople , *estTime;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.Res = [[NewReservation alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Property List.plist"];
if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]){
plistPath = [[NSBundle mainBundle] pathForResource:@"Property List" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSDictionary *temp = (NSDictionary *) [NSPropertyListSerialization propertyListWithData:plistXML options:NSPropertyListMutableContainersAndLeaves format:&format error:&errorDesc];
if(!temp){
NSLog(@"Error Reading plist: %@ , format: %lu",errorDesc , format);
}
name = [NSMutableArray arrayWithArray:[temp objectForKey:@"name"]];
phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"phoneNumbers"]];
noOfPeople = [NSMutableArray arrayWithArray:[temp objectForKey:@"noOfPeople"]];
estTime = [NSMutableArray arrayWithArray:[temp objectForKey:@"estTime"]];
//Confused how to go further
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
NewReservation.h
#import <UIKit/UIKit.h>
@interface NewReservation : UIViewController{
}
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *noOfPeople;
@property (weak, nonatomic) IBOutlet UITextField *estTime;
- (IBAction)nameReturn:(id)sender;
- (IBAction)enterDetails:(id)sender;
@end
NewReservation.m
#import "NewReservation.h"
@implementation NewReservation
- (IBAction)enterDetails:(id)sender{
if([self.name.text isEqualToString:@""] || [self.phoneNumber.text isEqualToString:@""] || [self.noOfPeople.text isEqualToString:@""] || [self.estTime.text isEqualToString:@""]) {
UIAlertView *error = [[UIAlertView alloc] initWithTitle:@"Oops" message:@"You must complete all fields!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[error show];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Property List.plist"];
//Cant continue here cause its confusing me, i need to add the values and update the list everytime a new 'name,noOfPersons,phoneNumber,estTime
//is entered.
}
@end
答案 0 :(得分:0)
实例变量对于类是唯一的。默认情况下,只有类和子类可以访问它。因此,作为面向对象编程的基本原理,实例变量(ivars)是私有的 - 它们由类封装。
相比之下,属性是一个公共值,可能与实例变量对应也可能不对应。如果你想公开一个ivar,你可能会做一个相应的财产。但与此同时,您希望保密的实例变量没有相应的属性,因此无法从类外部访问它们。您还可以使用与ivar不对应的计算属性...
如果没有属性,可以隐藏ivars。事实上,除非在公共标题中声明ivar,否则很难确定这样的ivar存在。
您的子课程的属性与ivar不对应 要使用属性访问ivar,ivar需要从子类中合成属性。
@implementation NewReservation
@synthesize phoneNumbers, name, noOfPeople, estTime;
// ****
@end