2013-04-10 13 views
1

我正在制作电梯的东西。我无法使用presentModalViewController发送具有不同视图的数据。我没有找到红色消息“favoriteColorString”属性。我复制了完全相同但不同的表单名称和按钮。 “favoriteColorString”出现错误,无法发送elevator2数据。使用协议和代理使用presentModalViewController不能在视图之间发送数据

我试过两种不同的东西。

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

而且

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

这里是我的代码:

ElevatorView.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 

ElevatorView.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

+0

1. post'Elevator2View'code。即使你不关心命名约定,也请不要将你的变量命名为类。 – Kreiri 2013-04-10 18:55:36

+0

这是Elevator2Viewcode的代码。 – HotScreen 2013-04-10 19:39:20

+0

也使用属性时(添加到@ Kreiri的评论),不要声明iVars,也不要合成。始终使用属性语法self.property引用它们。 [见这里](http://stackoverflow.com/questions/14236799/should-i-declare-variables-in-interface-or-using-property-in-objective-c-arc/14236931#14236931)了解详情。如果你用这些建议清理你的问题,这将有所帮助。 – foundry 2013-04-10 21:07:42

回答

0

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

摆脱这个错误很简单:只需将您的ivar Elevator2View *Elevator2View重命名为其他东西即可。事实上,Xcode 4.4和更新的版本会自动合成ivars以适应您的属性:如果您有属性propertyName,那么Xcode会自动合成ivar _propertyName。您的财产Elevator2View将拥有_Elevator2View伊娃。所以除非你真的需要有不同命名方案的ivars,否则你可以摆脱你的@synthesize,而且你也不需要为你的属性声明ivars。

(虽然我更喜欢以声明性质的ivars(以下Xcode的命名方案),因为过于频繁LLDB不显示autosynthesized-没有-宣布在检查对象实例变量。)

这是关于性能,高德和命名约定。但是你在这个代码中做什么?

-(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值的 - 你实例变量的 - 属性,然后创建Elevator2View和现在,模式视图控制器全新的对象。 (顺便说一下,presentModalViewController:animated:已在iOS 6.0中弃用)。当然,这个品牌Elevator2View对象不知道什么Elevator2View的(你的实例变量的)属性是!

相关问题