1

Recently I am stuck in a problem so that I cannot move on to the next coding part. There are two view controllers(let's say the first.m and second.m) and I would like to pass parameter data from one to another view controller without using storyboard. But the tricky part for me is the second view controller was already initiated in the AppDelegate.m file.

I can pass data if I have initiated the second view controller in the first view controller with some importing header file, for example..

@import "Second.h"

Second *secondView = [[Second alloc] initWithNibName:@"SecondNib" bundle:nil];
secondView.someStringValue = @"passThisString";

However, this coding makes a new second view controller, which is not what I want. I found there is a something like

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

to connect to the AppDelegate.m file. But after a few days struggling with this my brain is getting blackout. Please give me any advice.. Is there a way to pass data to existing view controller without using storyboard? or any way through AppDelegate to Second view controller. Any help will be appreciated

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
jazzed28
  • 89
  • 2
  • 7
  • So you are loading second view controller at first then your are loading first view controller. is it? – Ganapathy Jul 22 '13 at 07:09
  • There is no need to use AppDelegate or NSUserDefaults. Just create an init method to your second viewController. like initWithNibName but this method will take your someStringValue. – Samet DEDE Jul 22 '13 at 07:29
  • @Ganapathy I initiated two view controllers in the AppDelegate.m file. While I was working in the first.m I needed to pass some data to second.m – jazzed28 Jul 22 '13 at 09:54

3 Answers3

2

@jazzed28 you can do this without initiating your Second. You just need to access the already initiated Second. So declare Second in myAppDelegate.h as a property of it like this -

@property (nonatomic, strong) Second *svc;

then whenever you need second you can access like this -

myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.svc
saadnib
  • 11,145
  • 2
  • 33
  • 54
  • I was trying to update the values to other view controller without creating a new nib file, but all of sudden I figured out that would be an unnecessary thing. So I tried to push a new controller with a nib file through AppDelegate.h file. Thanks, I learned a lot with these answers – jazzed28 Jul 28 '13 at 22:25
1

crete two class methods in myAppDelegate for setting data and getting data from Appdelegate,you can access those methods by using APpdelegate class name.

 Setter method
 +(void)setGlobalString:(NSString *)stringgValue{
          [[NSUserDefaults standardUserDefaults] setObject:stringgValue forKey:@"globalValue"];
     // you can save string value here by using NSUserDefaults or any thing else

    }
Getter method
+(NSString *)getGlobalString{
        return [[NSUserDefaults standardUserDefaults] forKey:@"globalValue"];
    }

you can call methods like

[myAppDelegate setGlobalString:yourString];

NSString *str = [myAppDelegate getGlobalString];
Balu
  • 8,470
  • 2
  • 24
  • 41
1

This question has been asked many times before, including on StackOverflow. If you had spent some more time looking for an answer, you might have found this answer for example.

Community
  • 1
  • 1
ecotax
  • 1,933
  • 17
  • 22