私はここで立ち往生しています。Xcodeの2番目のビューから1番目のビューへの値の受け渡し
私はAppdelegateにNSStringを持っています。そして、私は最初のビューと呼ばれる2つのビューを持っています。最初のビューでは、私はラベルを持って、Appdelegateにあるstringvariableからテキストを設定します。最初のビューでボタンをクリックすると、secondviewに行きますサブビューとして表示)。2番目のビューでは、戻るボタンをクリックします。戻るボタンをクリックすると、最初のビューが表示されます(ここではappdelegate.Andにある値を設定しています)。[self.view removeFromSuperview]
です。最初のビューが表示されていますが、ラベルの値は更新されていません。どのボディもLabeltextを更新する方法を教えてください。親切に教えてください。
Appdelegate.h
#import <UIKit/UIKit.h>
@class FirstView;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *str1;
}
@property (nonatomic,retain) NSString *str1;
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
Appdelegate.m
#import "AppDelegate.h"
#import "FirstView.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize str1;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[FirsView alloc] initWithNibName:@"FirstView" bundle:nil] autorelease];
self.str1 = @"view1";
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
FirstView.h
#import <UIKit/UIKit.h>
#import "secondView.h"
@interface FirstView : UIViewController
{
IBOutlet UILabel *btn;
secondView *cont;
}
-(IBAction)gotoView2:(id)sender;
@end
FirstView.m
#import "FirstView.h"
-(IBAction)gotoView2:(id)sender
{
cont = [[secondView alloc] initWithNibName:@"secondView" bundle:nil];
[self.view addSubview:cont.view];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
**SecondView.h**
#import <UIKit/UIKit.h>
@interface SecondView : UIViewController
{
}
-(IBAction)goBack:(id)sender;
@end
SecondView
#import "SecondView.h"
#import "AppDelegate.h"
@implementation SecondView
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:@"Home"];
[self.view removeFromSuperView];
}
これをチェックしてください:http://oleb.net/blog/2012/02/passing-data-between-view-controllers/ –
どのようにAppdelegateのテキストを設定してコードを書いていますか? – Shubhank
Appdelegate * del = [[UIApplication sharedApplication] delegate]; del.str1 = @ "label2"; – Tendulkar