2012-04-20 91 views
1

我正在嘗試使用Xcode 4編寫我的第一個iPad應用程序:它基於具有兩個視圖的「選項卡式應用程序」模板。在第一個視圖中,用戶選擇一個城市(標籤顯示選擇),在第二個視圖中有一個IBOutletCollection(UITextField)NSArray * playersData。我想將第一個視圖中選定的城市設置爲第二個視圖中的默認城市。我檢查了故事板連接,他們似乎沒有問題。UITextField:從標籤設置文本

我什麼也沒得到。任何想法?

首先視圖:
#進口

@interface pruebaF1FirstViewController : UIViewController 
- (IBAction)selectCity:(UIButton *)sender; 
@property (weak, nonatomic) IBOutlet UILabel *citySelected; 
@end 

首先視圖實現: #進口 「pruebaF1FirstViewController.h」 #進口 「pruebaF1SecondViewController.h」

@interface pruebaF1FirstViewController() 
@property pruebaF1SecondViewController *secondView; 
@end 

@implementation pruebaF1FirstViewController 
@synthesize citySelected; 
@synthesize secondView; 

- (void)viewDidLoad 
... 

- (IBAction)selectCity:(UIButton *)sender { 
    NSString *currentCity =[sender currentTitle]; 
    [email protected]""; 
    citySelected.text =[citySelected.text stringByAppendingString:currentCity]; 

    /*writes null*/ 
    secondView.defaultCity.text=[NSString stringWithFormat:@"%@" ,currentCity]; 
    NSLog(@"%@",secondView.defaultCity.text); 
} 
@end 

第二視圖頭

#import <UIKit/UIKit.h> 
@interface pruebaF1SecondViewController : UIViewController <UITextFieldDelegate> 

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *playersData; 
@property (retain, nonatomic) IBOutlet UITextField *defaultCity; 

- (IBAction)eraseData:(UIButton *)sender; 
- (IBAction)savePlayersData:(UIButton *)sender; 

- (IBAction)termsPopUp:(UIButton *)sender; 

/*- (void)writeDefaultCity:(NSString *)currentCity;*/ 
@end 

第二視圖實現 #進口「pruebaF1SecondViewController.h」 #進口「pruebaF1FirstViewController.h」

@interface pruebaF1SecondViewController() 
@property (nonatomic, strong)pruebaF1FirstViewController *prueba; 
@end 

@implementation pruebaF1SecondViewController 
@synthesize playersData; 
@synthesize defaultCity; 
@synthesize prueba; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
    [self setPlayersData:nil]; 
    [self setDefaultCity:nil]; 
    [super viewDidUnload]; 
} 

/* Return or Done button dismiss keyboard*/ 
-(BOOL)textFieldShouldReturn:(UITextField *)boxes 
{ 
    for (UITextField *boxes in playersData) { 
     [boxes resignFirstResponder]; 
    } 
    return YES; 
} 

.... 

/*Trying to get city's name from first view in two ways*/ 

/*-(void)setDefaultCity 
{ 
NSString *defecto=prueba.citySelected.text; 
self.defaultCity.text = defecto; 
}*/ 
- (IBAction)eraseData:(UIButton *)sender { 

    for (UITextField *boxes in playersData) { 
     boxes.text = @" "; 
     } 
} 
/* 
-(void)writeDefaultCity:(NSString *)currentCity 
{  
    defaultCity.text =[NSString stringWithFormat:@"%@" ,currentCity]; 
    NSLog(@"Ciudad elegida: %@",currentCity); 
}*/ 
.... 
@end 

回答

2

視圖一般不加載,直到它們被顯示在設備上,並可以在任何時間時不被卸載可見,以節省內存。這意味着當您設置'defaultCity'的'text'屬性時,該標籤尚未創建。

相反,你應該在的NSString *屬性添加到pruebaF1SecondViewController並設置defaultCity標籤的viewDidLoad中值:

在標題:

@property (nonatomic, weak) UILabel *defaultCityLabel; 
@property (nonatomic, strong) NSString *defaultCity; 

在實施

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    defaultCityLabel.text = defaultCity; 
} 

在第一個視圖控制器中,分配字符串值而不是標籤值。

+0

感謝您的回答。我試圖通過按鈕或標籤顯示城市,用戶在第一個視圖中選擇第二個視圖上的文本字段作爲默認城市。 – Luis 2012-04-23 14:42:59

+0

解決! Extern NSString * myCity在PruebaF1Firs ... er.h中,我檢索了PruebaF1Second中的NSString值... er.m非常感謝! – Luis 2012-04-23 16:48:17