开发者

How to pass a string from one view to another in tab based app

I have created a tab based application having 4 tabs and 4 views respective to these tabs. I have a string in first view and when I printing this string in second view it printing null.

In first view.h

NSString *dateString;
@property(nonatomic,retain) NSString *dateString;

In first view.m

@synthesize dateString;
dateStr开发者_如何学Going=button6.titleLabel.text;
NSLog(@"dateString:%@",dateString);

In second view.h

NSString *dateString;
@property(nonatomic,retain) NSString *dateString;

In second view.m

@synthesize dateString;

- (void)viewDidLoad { [super viewDidLoad];
    NSLog(@"dateString:%@",self.dateString);
}


Add your view controllers as properties for the application delegate (if the app is a relatively simple design).

Then you can reference the properties of the second view controller from the first view controller, by way of the app delegate. (One such property could be the string you want the second VC to copy or retain.)


Create NSString variable in Application delegate class and set the Property and make synthesize that variable.

And set the @"" (blank) value in applicationDidFinishLaunching method.

For Example - my variable name is str, then initialize str in applicationDidFinishLaunching like self.str = [NSString stringWithFormat:@""];

And now you can use it in any tab *view* and set the value as per your require.

More code

AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    NSString *baseURL;
}

@property (nonatomic, retain) NSString *baseURL;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window;
@synthesize baseURL;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    self.baseURL = [NSString stringWithFormat:@""];
}

- (void)dealloc 
{
    [baseURL release];

    [window release];

    [super dealloc];
}

@end

ViewController1.h

@class AppDelegate;

@interface ViewController1 : UIViewController <UITextFieldDelegate>
{
    AppDelegate *appDelegate;
}

@end

ViewController1.m

#import "AppDelegate.h"
#import "ViewController1.h"

@implementation ViewController1

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        NSLog(@"value - %@",appDelegate.baseURL); // Here you can set or get the value.
}


it may not be the best answer.but creating a string variable in the appdelgate and passing the variable to this from the first view and fetching it from the second view works for me


Really, did we lose focus of MVC and the most awesome of abilities that is easy to do in iPhone Development?

How about a delegate?

@protocol ViewOneDelegate
- (void)getStringVariable;
@end

@interface ViewOneModel : NSObject
{
    NSString* _stringVariable;
    id<ViewOneDelegate> _theDelegate;
}

@property (nonatomic, retain) id<ViewOneDelegate> theDelegate;

@end

Assign a controller to be the delegate for the ViewOneModel.


Here is a simple solution, but not the best one, Create a global variable, and just use that.

Header

  extern NSString *GlobalString;
    @interface GlobalVariables : NSObject {
    }

@end

implementation

#import "GlobalVariables.h"

@implementation GlobalVariables
 NSString *GlobalString;
@end

And now to have access to the variable just import the header in the file you want to use. You'll probably want to check if it's initiated before you use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜