1

我正在做电梯的事情。我在使用 presentModalViewController 发送具有不同视图的数据时遇到问题。我收到红色消息“favoriteColorString”属性未找到。我复制了完全相同但不同的表单名称和按钮。“favoriteColorString”出现错误,无法发送电梯2数据。

我尝试了两种不同的东西。

  Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

  favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

这是我的代码:

电梯视图.h

#import <UIKit/UIKit.h>
#import "Elevator2View.h"

@interface ElevatorView : UIViewController<PassSecondColor>
 {

Elevator2View   *Elevator2View;

IBOutlet UITextField     *favoriteColorTextField;
IBOutlet UILabel         *favoriteColorLabel;
IBOutlet UILabel         *secondFavoriteColorLabel;

NSString        *secondFavoriteColorString;

}

@property (nonatomic, retain) Elevator2View *Elevator2View;
@property (nonatomic, retain) IBOutlet UITextField  *favoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;

@property (copy) NSString   *secondFavoriteColorString;

@end

电梯视图.m

#import "ElevatorView.h"
#import "Elevator2View.h"
    @implementation ElevatorView
@synthesize Elevator2View, favoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize secondFavoriteColorString;



-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

Elevator2View.h

#import <UIKit/UIKit.h>

@protocol PassSecondColor <NSObject>
@required
- (void) setSecondFavoriteColor:(NSString *)secondFavoriteColor;
@end

@interface Elevator2View : UIViewController{
IBOutlet UITextField *secondFavoriteColorTextField;
IBOutlet UILabel     *favoriteColorLabel;
IBOutlet UILabel     *secondFavoriteColorLabel;
NSString             *favoriteColorString;
id <PassSecondColor> delegate;

}

@property (copy) NSString   *favoriteColorString;



@property (nonatomic, retain) IBOutlet UITextField  *secondFavoriteColorTextField;
@property (nonatomic, retain) IBOutlet UILabel      *favoriteColorLabel;
@property (nonatomic, retain) IBOutlet UILabel      *secondFavoriteColorLabel;
@property (retain) id delegate;

@end

Elevator2View.m

#import "Elevator2View.h"

@interface Elevator2View ()

@end

@implementation Elevator2View
@synthesize secondFavoriteColorTextField, favoriteColorLabel, secondFavoriteColorLabel;
@synthesize favoriteColorString;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       // Custom initialization
   }
   return self;
}

- (void) viewWillAppear:(BOOL)animated
{
    favoriteColorLabel.text = favoriteColorString;  
}

- (void) viewWillDisappear:(BOOL) animated
{
//  [[self delegate] setSecondFavoriteColor:secondFavoriteColorTextField.text];
}


- (void)viewDidLoad
{
   [super viewDidLoad];
   // Do any additional setup after loading the view from its nib.
    favoriteColorLabel.text = favoriteColorString;  
}

- (void)didReceiveMemoryWarning
{
   [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

http://www.theappcodeblog.com/?p=90

4

1 回答 1

0

您的“找不到属性”的原因是您将 ivar 命名为与类相同。点表示法只是一种语法糖:object.property = value相当于[object setProperty:value]. 在 Objective C 中,类也是对象,当你调用时Elevator2View.favoriteColorString = whatever,Xcode 显然认为你正在尝试调用 类 Elevator2View 的方法setFavoriteColorString

摆脱这个错误很容易:只需将您的 ivar 重命名Elevator2View *Elevator2View为其他名称。事实上,Xcode 4.4 和更新的版本会为你的属性自动合成 ivar:如果你有一个 property propertyName,那么 Xcode 会自动合成 ivar _propertyName。您的财产Elevator2View将有_Elevator2Viewivar。因此,除非您真的需要具有不同命名方案的 ivars,否则您可以摆脱您的@synthesize,并且您也不需要为您的属性声明 ivars。

(虽然我更喜欢为属性声明 ivars(遵循 Xcode 命名方案),因为 lldb 经常不会在对象检查器中显示自动合成的不声明 ivars。)

那是关于属性、ivars 和命名约定的。但是你在这段代码中做了什么?

-(IBAction)level1:(id)sender;{
   favoriteColorTextField.text = @"1";
      Elevator2View.favoriteColorString = [[NSString alloc] initWithFormat:@"Your favorite color is %@", favoriteColorTextField.text];

      [self presentModalViewController:[[[Elevator2View alloc] init]
                                  autorelease] animated:NO];
}

您设置Elevator2View's 的值 - 您的实例变量的 - 属性,然后创建全新的Elevator2View 对象并将其呈现模态视图控制器。(顺便说一句,presentModalViewController:animated:在 iOS 6.0 中已弃用)。当然,这个全新的 Elevator2View对象不知道Elevator2View(你的实例变量的)属性是什么!

于 2013-04-11T07:19:37.017 回答