我正在尝试使用Xcode 4编写我的第一个iPad应用程序:它基于带有两个视图的“选项卡式应用程序”模板。在第一个视图中,用户选择一个城市(标签显示选择),在第二个视图中,有一个IBOutletCollection(UITextField)NSArray * playersData。我想将第一个视图中选择的城市设置为第二个视图中的默认城市。我检查了故事板连接,它们似乎没问题。
我一无所获。有什么想法吗?
第一种观点:
#import
@interface pruebaF1FirstViewController : UIViewController
- (IBAction)selectCity:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *citySelected;
@end
第一个视图实现: #import“pruebaF1FirstViewController.h” #import“pruebaF1SecondViewController.h”
@interface pruebaF1FirstViewController ()
@property pruebaF1SecondViewController *secondView;
@end
@implementation pruebaF1FirstViewController
@synthesize citySelected;
@synthesize secondView;
- (void)viewDidLoad
...
- (IBAction)selectCity:(UIButton *)sender {
NSString *currentCity =[sender currentTitle];
citySelected.text=@"";
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
第二个视图实现 #import“pruebaF1SecondViewController.h” #import“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
答案 0 :(得分:2)
视图通常在设备上显示之前不会加载,并且在不可见以节省内存时可以随时卸载。这意味着当您设置'defaultCity'的'text'属性时,尚未创建该标签。
相反,你应该为pruebaF1SecondViewController添加一个NSString *属性,并在viewDidLoad中设置defaultCity标签的值:
在标题中:
@property (nonatomic, weak) UILabel *defaultCityLabel;
@property (nonatomic, strong) NSString *defaultCity;
在实施中
- (void)viewDidLoad
{
[super viewDidLoad];
defaultCityLabel.text = defaultCity;
}
在第一个视图控制器中,指定字符串值而不是标签值。