0

I have NSObject class that has a NSString called tweetTitle;

TweetDesc.h file

#import <Foundation/Foundation.h>

@interface TweetDesc : NSObject

@property (strong, nonatomic) NSString * tweetTitle;

-(void) setFullTweetTitle:(NSString *) fullTweetTitle;
@end

TweetDesc.m file

@implementation TweetDesc
@synthesize tweetTitle;

-(void) setFullTweetTitle:(NSString *) fullTweetTitle
{
    self.tweetTitle = fullTweetTitle;
}
@end

I have three classes (View Controllers), FirstViewController, SecondViewController and ThirdViewController.

Here are the code of FirstViewController FirstViewController.h

@interface TweetViewController : UIViewController <UITextViewDelegate>
@property (strong, nonatomic) NSString * tweet;
@property (weak, nonatomic) IBOutlet UITextView *tvTweetTitle;
- (IBAction)btnCreateTweet:(id)sender;
@end

FirstViewController.m

@implementation TweetViewController
@synthesize tvTweetTitle, tweet;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tvTweetTitle.delegate = self;
}

- (IBAction)btnCreateTweet:(id)sender
{
    tweet = [[NSString alloc]initWithFormat:@"%@", tvTweetTitle.text];

    TweetDesc * td = [[TweetDesc alloc]init];
    [td setFullTweetTitle:tweet];

    SecondViewController * svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self.navigationController pushViewController:ivc animated:YES];
}

My Question is: How can I use tweetTitle in Second and third ViewController without creating new instance of TweetDesc class and set again the tweetTitle in every ViewControler.

In the second view controller I tried:

TweetDesc * td = [[TweetDesc alloc]init];
NSLog(@"%@", td.tweetTitle);

but I get null, it seems that it was released already or something else.

Thanks for the help in advance.

iMubarak
  • 462
  • 5
  • 13
  • for the secondViewController when u wrote `TweetDesc * td = [[TweetDesc alloc]init];` you still haven't assigned any value to the instance variable `tweetTitle` hence the null. If u write `[td setFullTweetTitle:@"some string"];` _THEN_ it is assigned a value. – Tushar Koul Apr 02 '13 at 08:28

6 Answers6

2

Just give SecondViewController a TweetDesc property, like this:

@interface TweetDesc : UIViewController

@property (nonatomic, strong) TweetDesc *tweetDesc;

...

Then, after you instantiate a SecondViewController, set its tweetDesc property:

SecondViewController * svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
svc.tweetDesc = td;
[self.navigationController pushViewController:svc animated:YES];

In your SecondViewController implementation, use self.tweetDesc to access the instance.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I liked this answer, and it worked fine on the second view controller. but it didn't work on the third view controller. The idea is to collect some data from view controller 1 and store it tweetDesc them move to view controller 2 and collect also some data and store them and finally retrieve them all in view controller 3. – iMubarak Apr 02 '13 at 09:20
1

First thing you don't need a class for a single property use the string only. There are several ways of doing this.

  • First you can have a tweet property globally defined in AppDelegate.
  • You can pass the variable reference from one to another viewcontrolleres.
  • Or, you can use NSUserDefaults to set and get the text.

In your case first one is more useful. Just do as follows

In AppDelegate.h

Define a property as NSString *tweetText;

Now in you action

- (IBAction)btnCreateTweet:(id)sender
{
     AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
     appDelegate.tweetText=tvTweetTitle.text;  
}

Then in any controller you want to access the value just use

AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog("%@",appDelegate.tweetText);

Same approach can be used for NSUserDefaults as to set value

[[NSUserDefaults standardUserDefaults] setObject:tvTweetTitle.text forKey:@"Tweet"];
[[NSUserDefaults standardUserDefaults] synchronize];

and to get

NSLog("%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"Tweet"]);

Make sure you set the values before navigating to other controllers..

The above should solve your purpose..

iphonic
  • 12,615
  • 7
  • 60
  • 107
1

Based on your comment I feel you want each of your view controllers to hold a TweetDesc object. For that you could have a base class like

@interface baseViewController:UIViewController
@property(strong, nonatomic)TweetDesc *td;
@end

All your viewcontrollers should derive from this base class. So that your controllers definition look like this -

@interface FirstViewController:baseViewController
...
@end

@interface SecondViewController:baseViewController
...
@end

@interface ThirdViewController:baseViewController
...
@end

U instantiate it in the FirstViewController -

TweetDesc * td = [[TweetDesc alloc]init];
[td setFullTweetTitle:@"whatever string you want"];

And pass it on to SecondViewController - //like in Rob Mayoff's answer

SecondViewController * svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
svc.tweetDesc = td;
[self.navigationController pushViewController:svc animated:YES];

INSIDE SecondViewController you may refer it as self.tweetDesc. And pass it on to whichever viewcontroller you want after this using the above code.

 ThirdViewController *third= [self.storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController "];
    third.tweetDesc = self.tweetDesc;
    [self.navigationController pushViewController:third animated:YES];

Pass it on the ANY viewcontroller just make sure it's base class is the baseClassViewController.

Tushar Koul
  • 2,830
  • 3
  • 31
  • 62
0

You need to use category of TweetDesc where you create method that adds your default title. Or you can create singleton of TweetDesc that will always hold one title.

NSDmitry
  • 460
  • 5
  • 12
  • 3
    Singleton is not a good idea, since he might want to have several `TweetDesc`s. It's a better idea to pass the pointer as a parameter. – Eli Ganem Apr 02 '13 at 08:30
0

In your ViewController create a method called initWithTweetDesc. The method will look like this:

- (id)initWithTweetDesc:(TweetDesc*)tweet
{
    self = [super init];
    if (self) {
        _tweet = tweet;
    }
    return self;
}

Now, when you want to move to the new view controller, run this code:

TweetViewController *vc = [[TweetViewController alloc] initWithTweetDesc:tweetDesc];
[self.navigationController pushViewController:vc animated:YES];
Eli Ganem
  • 1,479
  • 1
  • 13
  • 26
  • He's instantiating `SecondViewController` from a storyboard. Creating a custom init method won't work because he's got to use `instantiateViewControllerWithIdentifier:` to create it, not the custom init method. – rob mayoff Apr 02 '13 at 08:44
  • He already mentioned like: **without creating new instance of TweetDesc class and set again the tweetTitle in every ViewControler** Your solution need to declare a property of `TweetDesc` in every viewcontrollers. – Midhun MP Apr 02 '13 at 08:47
0

You can use your AppDelegate for this.

eclare the property @property(nonatomic, strong) TweetDesc * td; in AppDelegate.h file.

And change the method like:

- (IBAction)btnCreateTweet:(id)sender
{
    tweet = [[NSString alloc]initWithFormat:@"%@", tvTweetTitle.text];

    TweetDesc * td = [[TweetDesc alloc]init];
    [td setFullTweetTitle:tweet];
    [[[UIApplication sharedApplication] delegate] setTd:td];

    SecondViewController * svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self.navigationController pushViewController:ivc animated:YES];
}

And in your second and third view controllers you can get the value using:

TweetDesc *desc = [[[UIApplication sharedApplication] delegate] td];
NSLog(@"%@",desc.fullTweetTitle);
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 2
    Another bad idea. Why would he need to use the AppDelegate just to pass a simple pointer? What if he needs a second or third TweetDesc? Create 2 more properties in the AppDelegate? – Eli Ganem Apr 02 '13 at 08:41
  • @EliGanem: The OP just need to pass the Tweet object in second and third viewControllers, without declaring properties in those viewcontrollers. So I think this is one of the best way to do that. If he need more objects, he can either declare properties in each class or can declare an array in AppDelegate. I answered according to his question. If he needs much more he need to modify it. It's only a suggestion. – Midhun MP Apr 02 '13 at 08:45