将数据从一个视图传递到另一个视图

时间:2014-06-11 21:02:09

标签: ios objective-c uiwebview

我试图将数据从一个控制器传递到另一个控制器(不做任何类型的动画或segue)来填充带有数组的表。填充表不是问题,因为我能够做到这一点,但是使用tableview将数据传送到控制器已被证明是不可能的。

过去4天我一直试图弄清楚如何做到这一点。我已经阅读了大量有用的帖子,包括:Passing Data between View Controllers,绝对没有运气。我对iOS开发者来说相当新,所以也许我完全忽略了一些应该简单的东西。

我正在使用SWRevealViewController作为侧边栏。

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

2 个答案:

答案 0 :(得分:4)

尝试使用NSNotification或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

......在另一个班级的其他地方......

- (void) someMethod
{

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

}

source

的Singleton:

创建单身的标准方法就像......

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.单身无法发挥作用。

Singleton就像一个静态对象。你可能希望在这里保存整个应用程序可以访问的数据,而不需要对代码进行太多更改。(它就像一个全局对象),而NSNotification是自我解释和可转换的

答案 1 :(得分:0)

使用协议或NSNotification进行简单的数据传递..