1

Xcode 4を使用して最初のiPadアプリを作成しようとしています。これは、2つのビューを持つ「タブ付きアプリケーション」テンプレートに基づいています。最初のビューで、ユーザーは都市を選択し(ラベルは選択を表示します)、2番目のビューでIBOutletCollection(UITextField)NSArray*playersDataがあります。最初のビューで選択した都市を2番目のビューのデフォルトの都市として設定したいと思います。ストーリーボードの接続を確認しましたが、問題ないようです。

何も得られません。何か案が?

最初のビュー:
#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

2番目のビューヘッダー

#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

2番目のビューの実装#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;
}

そして、最初のView Controllerで、ラベル値の代わりに文字列値を割り当てます。

于 2012-04-23T08:23:58.523 に答える