0

テキスト フィールドに入力された値を次の ViewController の背景色に変更しようとしていますが、2 番目の View Controller で値を読み取ることができません。値を認識させるにはどうすればよいですか?

ViewController.h:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate,          UINavigationControllerDelegate>{    
IBOutlet UITextField *valueTextField;
}
- (IBAction)save:(id)sender;
@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
// Get the stored data before the view loads
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int value = [defaults integerForKey:@"value"];
NSString *valueString = [NSString stringWithFormat:@"%i",value];
// Update the UI elements with the saved data
valueTextField.text = valueString;

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)save:(id)sender {
[valueTextField resignFirstResponder];
int value = [[valueTextField text] integerValue];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:value forKey:@"value"];
[defaults synchronize];
NSLog(@"Data saved");
}

@end

NextViewController.h:

#import <UIKit/UIKit.h>
@class ViewController;
@interface NextViewController : UIViewController
@end

NextViewController.m:

#import "NextViewController.h"
#import "ViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

-(IBAction)displayClicks:(id)sender
{
if(value<20)
{
    self.view.backgroundColor = [UIColor redColor];
}
else {
    self.view.backgroundColor = [UIColor greenColor];
}

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
4

1 に答える 1

0

結局、最初は親メソッドを呼び出すのは良くありません。

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// Get the stored data before the view loads
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int value = [defaults integerForKey:@"value"];
NSString *valueString = [NSString stringWithFormat:@"%i",value];
// Update the UI elements with the saved data
valueTextField.text = valueString;

}

2番目のView Controllerのどこで、UserDefalutsから「値」の値を読み取りますか?

-(IBAction)displayClicks:(id)sender
{
if(value<20)
{
    self.view.backgroundColor = [UIColor redColor];
}
else {
    self.view.backgroundColor = [UIColor greenColor];
}

}

どこから if(value<20) をとりますか? 多分あなたは前にそれを取るシェルですか?

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int value = [defaults integerForKey:@"value"];
于 2012-11-09T07:32:05.533 に答える