问题描述
我被困在这里。
我在Appdelegate中有一个NSString。我有两个视图叫做firstview,第二个视图。在第一个视图中,我有一个标签,并在appdelegate中的stringvariable中设置文本。当我在第一个视图中单击按钮时,它转到第二个视图(将第二个视图添加到第一个视图)在第二个视图中,我有一个后退按钮。当我单击后退按钮时,它显示第一个视图(这里我设置appdelegate中的值。然后使用此 [self.view removeFromSuperview]
)。问题是第一个视图出现但是标签值没有更新。可以告诉我如何更新Labeltext.Kindly告诉我。
I have a NSString in Appdelegate. And I have two views called firstview,second view.In first view I have a label and set text from stringvariable which is in Appdelegate .When I click on the button in first view,It goes to the secondview(added the second view to first view as a subview).In second view I have a back button.when I click the back button it is displaying the first view ( Here I am setting the value which is in the appdelegate.And then used this [self.view removeFromSuperview]
).The Problem is first view is appearing but label value is not updating.Can any body tell me how to update the Labeltext.Kindly tell me.
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];
}
推荐答案
有一种模式如何做这样的事情。您应该定义这样的协议:
There is a pattern how to do such things. You should define a protocol like this:
@protocol FirstViewDelegate <NSObject>
- (void)didDismissSecondView;
@end
您的第一个视图应符合此协议:
Your first view should conform to this protocol:
@interface FirstView: UIViewController <FirstViewDelegate> {
...
在其实现中添加函数 didDismissSecondView
:
- (void) didDismissSecondView
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[btn setTitle:del.str1];
}
您的第二个视图必须有一个属性
Your second view has to have a property
@interface SecondView : UIViewController
{
id<FirstViewDeledate> delegate;
}
@property (nonatomic, retain) id<FirstViewDeledate> delegate;
当您从第一个视图显示第二个视图时,将其委托设置为 self
您的函数中的第一个视图
:
When you show your second view from the first view set its delegate to self
of first viewin your function:
-(IBAction)gotoView2:(id)sender
{
SecondView *aView = [[SecondView alloc] init] // or whatever
...//other initialization code
aView.delegate = self;
... // show it
}
和在您关闭第二个视图之前:
and before you dismiss the second view:
-(IBAction)gotoView1:(id)sender
{
AppDelegate *del = [[UIApplication sharedApplication] delegate];
[del setStr1:@"Home"];
[self.delegate didDismissSecondView];
[self.view removeFromSuperView];
}
你完成了。
有点长,但有效。
And you done.A bit long, but works.
这篇关于将值从第二个视图传递到Xcode中的firstView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!