无法访问父视图控制器的变量

时间:2013-07-15 17:55:26

标签: ios objective-c inheritance

我正在尝试应用基本的继承概念,我需要在子视图控制器中显示父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)

为什么我会变空?

3 个答案:

答案 0 :(得分:0)

这里的问题是您使用self.textEventName.text访问Parent.m中的textEventName属性,但您只在Child.m中使用textEventName.text。这没有意义 - 它等同于:

[[  textEventName] text];   // Error: you're not specifying the receiver of the message

如果要访问对象的属性,则需要指定哪个对象。在这种情况下,该对象为self

更改-displayParentStrings中的代码:

NSLog(@"DeleteFromCalendar Event Name %@", self.textEventName.text);
NSLog(@"DeleteFromCalendar  Event  %@", self.textEventLocation.text);

添加self.将解决您的问题。

答案 1 :(得分:0)

迦勒的答案将解决问题,但并非全部。真正的问题是您使用下划线前缀合成了属性。因此,当您执行textEventName.text时,您正在访问在Parent.h中声明的实例变量,而不是使用属性合成的变量。但是挂钩界面构建器以将UI元素分配给属性。因此,它们被分配给具有下划线前缀的那些。因此,没有下划线的实例变量是零。因此,您应该按如下方式更改代码。

Parent.h

@interface EditEventViewController : UITableViewController

// you don't need the instance variables

@property (nonatomic, weak) IBOutlet UITextField *textEventName;
@property (nonatomic, weak) IBOutlet UITextField *textEventLocation;
@property (nonatomic, weak) IBOutlet UITextField *textStartTime;
@property (nonatomic, weak) IBOutlet UITextField *textEndTime;
@property (nonatomic, weak) IBOutlet UITextField *textDate;
@property (nonatomic, weak) IBOutlet UITextField *textEndDate;

Parent.m

// you don't need to synthesize

//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 %@", self.textEventName.text);
    NSLog(@"DeleteFromCalendar  Event  %@", self.textEventLocation.text);
}

-(void)displayParentStrings
{
    NSLog(@"Display Parent Strings");
    NSLog(@"DeleteFromCalendar Event Name %@", _textEventName.text);
    NSLog(@"DeleteFromCalendar  Event  %@", _textEventLocation.text);
}

答案 2 :(得分:0)

您误认为子类概念(继承概念)与类的实例。他们完全没有关系。当您对类进行子类化时,您将创建一个新类,该类继承与该类关联的那些(某些?)属性。这里的关键字是你刚刚创建了一个新类。任何对象都可以创建这个子类的实例....而不仅仅是这个子类的父类。

这个概念非常适合所有面向对象的编程语言。在网上搜索“subclass vs instance”,你应该能够看到关于这个主题的更多讨论。祝你好运!