我一直在这里卡住。将值从第二个视图传递到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];
}
检查此:http://oleb.net/blog/2012/02/passing-data-between-view-controllers/ – 2012-03-14 05:17:09
你是如何设置Appdelegate中的文本给你的代码。 – Shubhank 2012-03-14 05:18:07
Appdelegate * del = [[UIApplication sharedApplication] delegate]; del.str1 = @“label2”; – Tendulkar 2012-03-14 05:29:20