将值从第二个视图传递到Xcode中的firstView

时间:2012-03-14 05:14:09

标签: iphone objective-c xcode

我被困在这里。

我在Appdelegate中有一个NSString。我有两个视图叫做firstview,第二个视图。在第一个视图中我有一个标签,并在appdelegate中的stringvariable中设置文本。当我在第一个视图中点击按钮时,它转到第二个视图(第二个视图添加到第一个视图)在第二个视图中我有一个后退按钮。当我单击后退按钮时,它显示第一个视图(这里我设置了appdelegate中的值。然后使用了这个[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];
}

1 个答案:

答案 0 :(得分:4)

有一种模式如何做这样的事情。您应该定义这样的协议:

@protocol FirstViewDelegate <NSObject>

- (void)didDismissSecondView;

@end

您的第一个视图应符合此协议:

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

在其实现中添加函数didDismissSecondView

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

    [btn setTitle:del.str1];
}

您的第二个视图必须具有属性

@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];
}

你做完了。 有点长,但有效。