gpt4 book ai didi

ios - 将数据从一个 View 传递到另一个 View

转载 作者:行者123 更新时间:2023-11-28 19:01:57 25 4
gpt4 key购买 nike

我正在尝试将数据从一个 Controller 传递到另一个 Controller (不执行任何类型的动画或 segue)以使用数组填充表。填充表格不是问题,因为我已经能够做到这一点,但事实证明,使用表格 View 将数据发送到 Controller 是不可能的。

在过去的 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

最佳答案

尝试使用 NSNotification 或 Singleton

NS通知:

@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

...另一个类(class)的其他地方...

- (void) someMethod
{

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

}

source

单例:

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

单例.h

@interface MySingleton : NSObject

+ (MySingleton*)sharedInstance;

@end

单例.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.单例不可能不工作。

单例就像一个静态对象。你会想在这里保存数据,整个应用程序都可以访问这些数据,而无需对代码进行太多更改。(它就像一个全局对象)而 NSNotification 是 self 解释和谷歌搜索

关于ios - 将数据从一个 View 传递到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com