2012-03-14 64 views
1

我一直在这里卡住。将值从第二个视图传递到Xcode中的firstView

我在Appdelegate中有一个NSString。我有两个视图称为firstview,第二视图。在第一视图中,我有一个标签,并从AppDelegate中的字符串变量设置文本。当我点击第一个视图中的按钮时,它转到第二个视图(第二个视图添加到第一个作为子视图)。在第二个视图中,我有一个后退按钮。当我单击后退按钮时,它显示第一个视图(这里我设置了appdelegate中的值,然后使用这个[self.view removeFromSuperview])。问题是第一个视图出现,但标签值不更新。可以任何机构告诉我如何更新Labeltext.Kindly告诉我。

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/ – 2012-03-14 05:17:09

+0

你是如何设置Appdelegate中的文本给你的代码。 – Shubhank 2012-03-14 05:18:07

+0

Appdelegate * del = [[UIApplication sharedApplication] delegate]; del.str1 = @“label2”; – Tendulkar 2012-03-14 05:29:20

回答

4

是有规律可循如何做这样的事情。你应该这样定义一个协议:

@protocol FirstViewDelegate <NSObject> 

- (void)didDismissSecondView; 

@end 

你的第一个观点应符合本协议:

- (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; 

当你告诉你的第一个视图第二种观点在功能设置其委托第一种观点 的self

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

} 

和您关闭第二视图之前:

-(IBAction)gotoView1:(id)sender 
{ 

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

    [self.delegate didDismissSecondView]; 

    [self.view removeFromSuperView]; 
} 

你做。 有点长,但有效。

+0

谢谢兄弟我得到了解决方案 – Tendulkar 2012-03-15 05:54:01