我正在嘗試使用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
感謝您的回答。我試圖通過按鈕或標籤顯示城市,用戶在第一個視圖中選擇第二個視圖上的文本字段作爲默認城市。 – Luis 2012-04-23 14:42:59
解決! Extern NSString * myCity在PruebaF1Firs ... er.h中,我檢索了PruebaF1Second中的NSString值... er.m非常感謝! – Luis 2012-04-23 16:48:17