我想应用一个基本的继承概念,我需要在子视图控制器中显示父viewcontorller的textfield.text
的。但是我得到空值不能访问父视图控制器的变量
Parent.h
@interface EditEventViewController : UITableViewController
{
UITextField *textEventName;
UITextField *textEventLocation;
UITextField *textStartTime;
UITextField *textEndTime;
UITextField *textDate;
UITextField *textEndDate;
//tried IBOutlet version such as
//IBOutlet UITextField *textEventName;//this didnt work too
}
@property (nonatomic, strong) IBOutlet UITextField *textEventName;
@property (nonatomic, strong) IBOutlet UITextField *textEventLocation;
@property (nonatomic, strong) IBOutlet UITextField *textStartTime;
@property (nonatomic, strong) IBOutlet UITextField *textEndTime;
@property (nonatomic, strong) IBOutlet UITextField *textDate;
@property (nonatomic, strong) IBOutlet UITextField *textEndDate;
Parent.m
@synthesize textEndDate=_textEndDate;
@synthesize textDate=_textDate;
@synthesize textEndTime=_textEndTime;
@synthesize textEventName=_textEventName;
@synthesize textStartTime=_textStartTime;
@synthesize textEventLocation=_textEventLocation;
//test inherritance here
DeleteFromCalendar *deleteControl=[[DeleteFromCalendar alloc] init];
NSLog(@"Delete Request for text name %@",self.textEventName.text);
[deleteControl displayParentStrings];
Child.h
#import "EditEventViewController.h"
@interface DeleteFromCalendar : EditEventViewController
-(void)displayParentStrings;
@end
Child.m
-(void)displayParentStrings
{
NSLog(@"Display Parent Strings");
NSLog(@"DeleteFromCalendar Event Name %@",textEventName.text);
NSLog(@"DeleteFromCalendar Event %@",textEventLocation.text);
}
的NSLog:
Delete Request for text name Aeronautical Knowledge Review
DeleteFromCalendar Event Name (null)
DeleteFromCalendar Event Lcoation (null)
为什么我得到空?
你是否已经将界面元素上钩到界面构建器的插座?此外,如果在父级中声明属性,则不需要综合该属性。 (并且在Xcode 4中,无论如何您都不需要综合属性 - 现在这是自动的。) –
不要再声明child.m中的属性,它将覆盖父属性。也不要将变量声明为属性和类成员,只能使用其中的一个。 )。你也不需要在child.h文件中声明该方法,它已经在parrent中声明了。 – danypata
对不起复制粘贴错误,didnt儿童在IB重新申报文本框 –