0

我正在尝试应用一个基本的继承概念,我需要textfield.text在子视图控制器中显示父 viewcontorller 的。但我得到空值

父母.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;

父母.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];

孩子.h

#import "EditEventViewController.h"

@interface DeleteFromCalendar : EditEventViewController

-(void)displayParentStrings;

@end

孩子.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)

为什么我会得到空值?

4

3 回答 3

0

这里的问题是您self.textEventName.text用于访问 Parent.m 中的textEventName属性,但您仅textEventName.text在 Child.m 中使用。这是没有意义的——它相当于:

[[  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.将解决您的问题。

于 2013-07-15T18:25:30.280 回答
0

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

父母.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;

父母.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];

孩子.h

#import "EditEventViewController.h"

@interface DeleteFromCalendar : EditEventViewController

-(void)displayParentStrings;

@end

孩子.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);
}
于 2013-07-15T18:50:01.267 回答
0

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

这个概念非常适用于所有面向对象的编程语言。在网上搜索“子类与实例”,您应该能够看到有关此主题的更多讨论。祝你好运!

于 2013-07-17T09:34:39.420 回答