2013-04-10 16 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的(你的實例變量的)屬性是!

相關問題