Tendulkar
Tendulkar

Reputation: 5540

Passing values from second view to firstView in Xcode

I have been stuck here.

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

Upvotes: 0

Views: 3332

Answers (1)

Nava Carmon
Nava Carmon

Reputation: 4533

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> {
...

in its implementation add function 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;

When you show your second view from the first view set its delegate to self of first view in 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.

Upvotes: 4

Related Questions