简体   繁体   English

将数据从NSObject发送到UIViewController

[英]Sending data from NSObject to UIViewController

I have been working on this problem for close to 4 days now. 我已经在这个问题上工作了近4天。

I am at the point where I think its not so much a problem with my code, but the structure of my application that is causing the issue. 我认为我的代码并不是问题,而是导致问题的应用程序结构的问题。

I am trying to implement protocols and delegates to get an array from one NSObject(class) to a ViewController. 我正在尝试实现协议和委托,以将一个数组从一个NSObject(class)传递到ViewController。

my code is pretty much line by line copied from this tutorial the only differences are in the face I have ARC turned on so have had to replace (nonatomic, retain) to (strong) and have not used dealloc :) 我的代码是从本教程中逐行复制的,唯一的区别是我打开了ARC,所以不得不将(非原子,保留)替换为(强),并且没有使用过dealloc :)

so with that being said its still not passing the data back to the viewcontroller. 因此,据说它仍然不会将数据传递回ViewController。 (highly annoying) I have tried dozens of different combinations of solutions that I have had help with and nothing has worked. (非常烦人)我尝试了数十种我曾帮助过的解决方案的不同组合,但没有任何效果。 This has lead me to believe that maybe there is an error in the structure of my application or the way things have been initialized etc, which I will attempt to explain now. 这使我相信,我的应用程序的结构或事物初始化的方式等中可能存在错误,我将尝试解释一下。

When my viewcontroller with tableview loads the viewdidload method called the delegate of my parser class, then once the first cell of the tableview has loaded it called my connection class and tells it to download some data from the server. 当我的带有表视图的viewcontroller加载称为解析器类的委托的viewdidload方法时,一旦表视图的第一个单元格加载完毕,它便称为连接类,并告诉其从服务器下载一些数据。 Inside my connection class I use NSURLConnection delegates from the apple library, in the delegate method connectionDidFinishLoading the data that has been downloaded is passed over to my parser class (however this is where i think its going wrong because i declare the object again.. which i think is where things are going amiss) 在我的连接类内部,我使用苹果库中的NSURLConnection委托,在委托方法connectionDidFinishLoading中,将已下载的数据传递给解析器类(但是这是我认为出错的地方,因为我再次声明了该对象。我认为事情出了差错)

this is how I call my parser class from my connection class. 这就是我从连接类中调用解析器类的方式。

parserClass *myparser = [[EngineResponses alloc] init];
[myparser  ReciveResponse:receivedData];

then once the data is in my parser class it gets parsed and then I try to pass the data across to my viewcontroller.. but its never accessing that delegate method that I set up. 然后,一旦数据进入了我的解析器类,就对其进行了解析,然后我尝试将数据传递给我的viewcontroller ..但它从不访问我设置的委托方法。

Hopefully this is where the problem is because I just dont know where else I am going wrong. 希望这是问题所在,因为我只是不知道我要去哪里。 what do you think? 你怎么看?

UPDATE: heres my code - 更新:这是我的代码-

ViewController.h ViewController.h

#import "EngineResponses.h" //delegates & protocols

interface SearchViewController : UITableViewController <PassParsedData> {

//delegates to parser class
    EngineResponses *engineResponses;
//..

ViewController.m ViewController.m

#import "EngineResponses.h"

//this is where I set up the delegate/protocol for the parser class
- (void)viewDidLoad
{
    [super viewDidLoad];

//..
engineResponses = [[EngineResponses alloc] init];
[engineResponses setMydelegate:self];
//..
}

//this is where i set up and call the connection class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
if(indexPath.section == 0){        
    //..
    if (indexPath.row == 0){
     EngineRequests *engineRequests = [[EngineRequests alloc] init];
                [engineRequests initalizePacketVariables:0 startCode:@"myReg" activationCode:@"myAct" methodName:@"GetStuff"];
                //..
}

#pragma - Reciver methods

- (void)sendArray:(NSArray *)array
{
    ICMfgFilterArray = array;
    [self.tableView reloadData]; 
}

EngineRequests.m EngineRequests.m

//connection delegates etc..
//then I pass the data from the connection delegates over to the parser class
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{   
    EngineResponses *engineResponses = [[EngineResponses alloc] init];
    [engineResponses  ReciveResponse:receivedData];
}

EngineResponses.h EngineResponses.h

@protocol PassParsedData
- (void)sendArray:(NSArray *)array;
@end

//..
id <PassParsedData> mydelegate;
//..
@property (strong) id  <PassParsedData> mydelegate; 

EngineResponses.m EngineResponses.m

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//..
    [[self mydelegate]sendArray:filteredArray];    
}

1 1个

Allright. 好吧。 I will re-do it based on your updated code. 我将根据您更新的代码重新执行此操作。 To make it easy I copy your code and do the amendments. 为了简单起见,我复制您的代码并进行修改。

ViewController.h ViewController.h

#import "EngineResponses.h" //delegates & protocols

interface SearchViewController : UITableViewController <PassParsedData> {

//delegates to parser class
    EngineResponses *engineResponses;

    EngineRequests *engineRequests;  
//..

Explanation: You are using ARC. 说明:您正在使用ARC。 If you define the pointer locally, as you did before, and to not retain it - which you can't because of ARC - then it will be released directly after its creation. 如果像以前一样在本地定义指针,并且不保留指针(由于ARC而不能保留指针),则它将在创建指针后立即释放。 You will have to keep at least one reference to the object. 您将必须保留对该对象的至少一个引用。 Bare in mind that ARC means Automatic Reference Counting. 切记ARC意味着自动引用计数。 As soon as there is no reference to an object it will be released. 一旦没有对对象的引用,它将被释放。 This proposal with the engineRequests object defined here only works while you submit only one request at a time. 仅在您一次仅提交一个请求的情况下,此定义了engineRequests对象的提议才有效。 If you have several requests, ie for more than one cell or whatver, then you may go for a mutable array or mutable dictionary where you keep the requests while you use them. 如果您有多个请求,即对于多个单元格或多个单元格,则可以使用可变数组或可变字典,在使用它们时将请求保留在其中。

ViewController.m ViewController.m

#import "EngineResponses.h"

//this is where I set up the delegate/protocol for the parser class
- (void)viewDidLoad
{
    [super viewDidLoad];

//..
engineResponses = [[EngineResponses alloc] init];
[engineResponses setMydelegate:self];

engineRequests = [[EngineRequests alloc] init];  // Use instance variable instead of local variable
[engineRequests setEnineResponses:engineResponses];

//..
}

//this is where i set up and call the connection class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
if(indexPath.section == 0){        
    //..
    if (indexPath.row == 0){
     [engineRequests initalizePacketVariables:0 startCode:@"myReg" activationCode:@"myAct" methodName:@"GetStuff"];
                //..
}

#pragma - Reciver methods

- (void)sendArray:(NSArray *)array
{
    ICMfgFilterArray = array;
    [self.tableView reloadData]; 
}

Explanation: The engineRequets is now an instance varaible and should not be re-defined locally. 说明:engineRequets现在是一个实例变量,不应在本地重新定义。 You could define a variable of the same name locally which would hide the instance variable. 您可以在本地定义一个同名变量,该变量将隐藏实例变量。 I think in that case you get a compiler warning but that will work and will most probably confuse you. 我认为在这种情况下,您会收到编译器警告,但这将起作用并且很可能使您感到困惑。 Again, if you use more than one request at a time then this solution will not work! 同样,如果您一次使用多个请求,则此解决方案将无法使用!

EngineRequests.h EngineRequests.h

EngineResponses *engineResponses;

EngineRequests.m EngineRequests.m

@synthesize engineResponses;

//connection delegates etc..
//then I pass the data from the connection delegates over to the parser class
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{   
    //EngineResponses *engineResponses = [[EngineResponses alloc] init]; //This Object has already been created! 
    [engineResponses  ReciveResponse:receivedData];
}

Explanation: Here, too, the reference to EngineResponses is now an instance variable, not a locally defined one. 说明:在这里,对EngineResponses的引用现在也是一个实例变量,而不是本地定义的实例变量。 The object will not be newly created but it references to that very object that was created in the view controller. 该对象将不会被新创建,但会引用在视图控制器中创建的那个对象。 That is the one EngineResponses that 'knows' its view controller object and can therefore pass back the parsed data. 这是一个“知道”其视图控制器对象并因此可以传递回解析的数据的EngineResponses。

EngineResponses.h EngineResponses.h

@protocol PassParsedData
- (void)sendArray:(NSArray *)array;
@end

//..
id <PassParsedData> mydelegate;
//..
@property (strong) id  <PassParsedData> mydelegate; 

EngineResponses.m EngineResponses.m

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//..
    [[self mydelegate]sendArray:filteredArray];    
}

... give it a try :) ... 试试看 :)

Always check for nil objects. 始终检查零对象。 Sending a message to a nil object will do nothing and your app will continue. 向nil对象发送消息将无济于事,您的应用将继续。 I bet this is the problem since you are locally allocing all over the place. 我敢打赌这是问题所在,因为您是在本地分配所有位置。 Why dont you make the receivedata method a static method instead since it looks like you dont need these classes for more than a few moments for some calculations and parsing. 为什么不将Receivedata方法设置为静态方法呢?因为看起来好像不需要一些时间来进行一些计算和解析,所以您为什么不使它们成为静态方法呢? Then nil objects wont be a factor. 然后,零个对象将不是一个因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Objective-C –将数据从NSObject子类发送到UIViewController - Objective-C – Sending data from NSObject subclass to an UIViewController 将NSString从NSObject中的didUpdateLocations传递到UIViewController - Pass NSString from didUpdateLocations in NSObject to a UIViewController 如何从iOS中的NSObject类返回到UIViewController - How to back to UIViewController from a NSObject Class in iOS 从NSObject类到UIViewController类获取NSMutableArray时的内存管理 - Memorymanagement when getting a NSMutableArray from NSObject class to UIViewController class 我需要使用委托将字符串从NSObject类传递给UIViewController - I need to pass a string from a NSObject class to a UIViewController with a delegate NSObject 和 UIViewController 有什么区别 - what is the difference between NSObject and UIViewController 将数据从UIViewController传递回1个UIViewController - Passing data from UIViewController to 1 back UIViewController iPhone-从一个UIViewController向另一个发送动作? - iPhone - sending an action from one UIViewController to another? 调用presentModalViewController时NSObject中的UIViewController不显示 - UIViewController in NSObject not presenting when calling presentModalViewController 如何使用通用(NSObject)控制器与UIViewController的子视图? - How to use generic (NSObject) controller with subviews of a UIViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM