1

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

1 回答 1

2

视图通常在它们显示在设备上之前不会加载,并且可以在不可见时随时卸载以节省内存。这意味着当您设置“defaultCity”的“text”属性时,该标签尚未创建。

相反,您应该将 NSString * 属性添加到 pruebaF1SecondViewController 并在 viewDidLoad 中设置 defaultCity 标签的值:

在标题中:

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

在实施中

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

在第一个视图控制器中,分配字符串值而不是标签值。

于 2012-04-23T08:23:58.523 回答