Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing data between tabs in iOS

it is my first app that I am trying to do on my own and I have some questions. I want to have 4 tabs, and in the first one named "HomeView" I am parsing JSON data (this is done so far).

But what I want is some of the data that are being parsed to be visible in other tabs (and not have to parse them again).

So parts of my code of HomeView is here:

#import "HomeView.h"

@interface HomeView ()
@end

@implementation HomeView


//other code

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//data fetched
parsed_date=[res objectForKey:@"date"];
 NSLog(@"Date:%@",parsed_date);
[UIAppDelegate.myArray  addObject:parsed_date];
        }

and I can see the "parsed_date" being printed out correctly.

So I want this parsed_date to be visible in OtherView.

This is my code but I cannot print it out.

OtherView.m

#import "OtherView.h"
#import "HomeView.h"
#import "AppDelegate.h"

@interface OtherView ()

@end

@implementation OtherView
@synthesize tracks_date;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //preview value of other class
   tracks_date = [UIAppDelegate.myArray objectAtIndex:0];
NSLog(@"Dated previed in OtherView: %@", tracks_date);
}

and (null) is being printed out.

added code of app delegate.h

#import <UIKit/UIKit.h>

#define UIAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) NSArray *myArray;

@end

So can you suggest me a sollution?

like image 735
ghostrider Avatar asked Aug 27 '12 15:08

ghostrider


People also ask

What is tab bar controller in Swift?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


2 Answers

Add the property to your Application Delegate instead.

When assigning the property do something like:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];

delegate.myProperty = @"My Value";

then, in your different tabs, you can retrieve this property in the same manner:

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *valueInTab = delegate.myProperty; 
like image 156
scord Avatar answered Oct 13 '22 12:10

scord


Uh, when you create a HomeView in your last code segment there, you're creating a new object -- a new instance of the class. It will not contain the data from connectionDidFinishLoading unless that method is executed in that instance of the class.

You basically need to use some sort of persistence mechanism to do what you want, either the AppDelegate or static storage along the lines of a "singleton".

like image 22
Hot Licks Avatar answered Oct 13 '22 14:10

Hot Licks