2012-03-14 10 views
1

私はここで立ち往生しています。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]; 
} 
+0

これをチェックしてください:http://oleb.net/blog/2012/02/passing-data-between-view-controllers/ –

+0

どのようにAppdelegateのテキストを設定してコードを書いていますか? – Shubhank

+0

Appdelegate * del = [[UIApplication sharedApplication] delegate]; del.str1 = @ "label2"; – Tendulkar

答えて

4

ようなことを行うにはどのようにパターンがあります。あなたの最初のビューは、このプロトコルに準拠している必要があり

@protocol FirstViewDelegate <NSObject> 

- (void)didDismissSecondView; 

@end 

:あなたは、このようなプロトコルを定義する必要がありますあなたの2番目のビューがしなければならない

- (void) didDismissSecondView 
{ 
    AppDelegate *del = [[UIApplication sharedApplication] delegate]; 

    [btn setTitle:del.str1]; 
} 

@interface FirstView: UIViewController <FirstViewDelegate> { 
... 

その実装に機能didDismissSecondViewを追加プロパティを持っている

@interface SecondView : UIViewController 
{ 
    id<FirstViewDeledate> delegate; 
} 
@property (nonatomic, retain) id<FirstViewDeledate> delegate; 
あなたが表示された場合

は、第一の視点から2番目のビューには、関数内で最初のビュー のselfにそのデリゲートを設定します。

-(IBAction)gotoView1:(id)sender 
{ 

    AppDelegate *del = [[UIApplication sharedApplication] delegate]; 
    [del setStr1:@"Home"]; 

    [self.delegate didDismissSecondView]; 

    [self.view removeFromSuperView]; 
} 

そして、あなたが行わ:

-(IBAction)gotoView2:(id)sender 
{ 
    SecondView *aView = [[SecondView alloc] init] // or whatever 
    ...//other initialization code 
    aView.delegate = self; 
    ... // show it 

} 

を、あなたは2番目のビューを閉じる前に、 。 少し長いですが、動作します。

+0

おかげで、私は解決策を得た – Tendulkar

関連する問題