views:

371

answers:

1

I have two UIViewcontroller classes named First and second...When i clicked firstview button,i passed one string to secondview and display that string into secondview uitextview...But when i go to secondview there is no text in the textview...

I posted code....

What i am missing here

//first.h
#import <UIKit/UIKit.h>


@interface FirstViewController : UIViewController {
    IBOutlet UIButton *btnView;
}

@property(nonatomic,retain) UIButton *btnView;

-(IBAction)UpdateSecondView:(id)sender;


@end

//first.m
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "testtabbarAppDelegate.h"


@implementation FirstViewController
@synthesize btnView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
     // Initialization code
    }
    return self;
}

/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */


- (void)viewDidLoad {

}

-(IBAction)UpdateSecondView:(id)sender
{
    SecondViewController *object=[[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil];
    [object UpdateText:@"Testing"];
    [object release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [btnView release];
    [super dealloc];
}


@end

//second.h
#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController {
    IBOutlet UITextView *textView;
    NSString *globalString;
}
@property(nonatomic,copy) NSString *globalString;
@property(nonatomic,retain)  UITextView *textView;

-(void)UpdateText:(NSString *)string;

@end
//second.m
//
//  SecondViewController.m
//  testtabbar
//
//  Created by mac user on 6/9/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SecondViewController.h"


@implementation SecondViewController
@synthesize textView;
@synthesize globalString;


//NSString *globalString=@"";

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
     // Initialization code

    }
    return self;
}

/*
 Implement loadView if you want to create a view hierarchy programmatically
- (void)loadView {
}
 */

- (void)viewDidLoad 
{   

}

-(void)UpdateText:(NSString *)string
{
    textView.text=string;
}

- (void)viewWillAppear:(BOOL)animated
{
}

- (BOOL)textViewShouldReturn:(UITextView *)theTextView {
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    if (theTextView == textView) {
     [textView resignFirstResponder];
    }
    return YES;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [globalString release];
    [textView release];
    [super dealloc];
}


@end

Can anyone help me?

Thanks in advance...

+1  A: 

I am going to make the assumption both of these are implemented as nibs, and are in a UITabBarController.

I think you are confusing the concept of a class and an instance. A class is (roughly speaking) a description of some code and state. You can use that description to create an instance of that class (which is referred to as instantiation). Every instance of that class is unique and separate, if you modify one instance it does not effect the others.

Okay, so why I am going into this? Well, FirstViewController and SecondViewController are classes, not instances. The nib files you have describe the configuration of a particular instance of those controllers, and when it is loaded it instantiates the the controller with those settings. What you are doing in UpdateSecondView is creating ANOTHER SecondViewController, setting a value in it, then immediately disposing of it. What you need to do is set the value in the instance of SecondViewController that is being built when the nib is loaded.

Without actually seeing your nibs and exactly how things are laid out I can't give you an an exact explanation of what you need to do, but the basic gist is you want to make an instance variable in FirstViewController and hook that up to the appropriate instance of SecondViewController, than use that to modify that instance of SecondViewController like this:

//Updated @interface
@interface FirstViewController : UIViewController {
    IBOutlet UIButton *btnView;
    IBOutlet SecondViewController *secondViewController;
}

@property (nonatomic, retain) SecondViewController *secondViewController;
@end

//Updated @implementation
-(IBAction)UpdateSecondView:(id)sender
{
    [self.secondViewController UpdateText:@"Testing"];
}

Now if you just run the with the above changes it won't work (because secondViewController will not be set), so like I said before you will need to have some piece of your code that has knowledge of both instances hook it up. You can potentially do that in the nib for the UITabBarController that is housing the both by dragging a link to the new IBOulet there, or you could do it programmatically somewhere else.

Louis Gerbarg