我正在尝试使用 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