theseanstewart
theseanstewart

Reputation: 65

Pass data from one view to another

I am trying to pass data from one controller to another (without doing any type of animation or segue) to populate a table with an array. Populating the table is not an issue as I have been able to do that, but getting the data to the controller with the tableview has proven to be impossible.

I've been trying to figure out how to do this for the last 4 days. I've read through tons of helpful posts including: Passing Data between View Controllers, with absolutely no luck. I'm fairly new to iOS dev so maybe I'm just completely overlooking something that should be simple.

I am using SWRevealViewController for the sidebar.

WebAppViewController.m

- (void)webViewDidFinishLoad:(UIWebView *)webView
{

NSString *html = [webView stringByEvaluatingJavaScriptFromString:
                  @"document.getElementById('json-ios').innerHTML"];
NSData *jsonData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];

NSArray *jsonArray = [dict objectForKey:@"results"];

// This is where I would like to pass jsonArray to my sidebar to populate a table (SidebarViewController) each time the page loads
.....

enter image description here

Upvotes: 0

Views: 157

Answers (2)

penduDev
penduDev

Reputation: 4795

try using NSNotification or Singleton

NSNotification:

@implementation TestClass

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (!self) return nil;

    // Add this instance of TestClass as an observer of the TestNotification.
    // We tell the notification center to inform us of "TestNotification"
    // notifications using the receiveTestNotification: selector. By
    // specifying object:nil, we tell the notification center that we are not
    // interested in who posted the notification. If you provided an actual
    // object rather than nil, the notification center will only notify you
    // when the notification was posted by that particular object.

    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(receiveTestNotification:) 
        name:@"TestNotification"
        object:nil];

    return self;
}

- (void) receiveTestNotification:(NSNotification *) notification
{
    // [notification name] should always be @"TestNotification"
    // unless you use this method for observation of other notifications
    // as well.

    if ([[notification name] isEqualToString:@"TestNotification"])
        NSLog (@"Successfully received the test notification!");
}

@end

... somewhere else in another class ...

- (void) someMethod
{

    // All instances of TestClass will be notified
    [[NSNotificationCenter defaultCenter] 
        postNotificationName:@"TestNotification" 
        object:self];

}

source

Singleton:

The standard way of creating a singleton is like...

Singleton.h

@interface MySingleton : NSObject

+ (MySingleton*)sharedInstance;

@end

Singleton.m

#import "MySingleton.h"

@implementation MySingleton

#pragma mark - singleton method

+ (MySingleton*)sharedInstance
{
    static dispatch_once_t predicate = 0;
    __strong static id sharedObject = nil;
    //static id sharedObject = nil;  //if you're not using ARC
    dispatch_once(&predicate, ^{
        sharedObject = [[self alloc] init];
        //sharedObject = [[[self alloc] init] retain]; // if you're not using ARC
    });
    return sharedObject;
}

@end

source

P.S.There is no way singleton won't work.

Singleton is like a static object. You would want to save data here that will be accessible by the whole app without much changes in code.(Its like a global object) whereas NSNotification is self explanatory and googlable

Upvotes: 4

rambo
rambo

Reputation: 61

Use protocol or NSNotification for simple data passing ..

Upvotes: 0

Related Questions