1

I have a very complex situation (well for me) I am trying to resolve but thus far am having trouble with it.

I will outline the structure of the application now and then explain the problem I am having. The names I am using are made up due to sensitivity of the data I am using.

secondToLastViewController // is a UITableView on the navigation stack
lastViewController // is just a normal UIView that i want to push onto the navigation stack
RequestClass // this class dose requests to my database and passed the data back to correct classes
getInfoClass // class is used for this specific request stores the information correctly and passes it back to secondToLastViewController

So as the user initiates didSelectRowAtIndexPath inside secondToLastViewController I make a request for the data using the RequestClass

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//..
[RequestClass Getinfo:storedInfoPram];
}

now the thread shoots off to my RequestClass, which in turn queries the DB for some data which is then received and this data is passed off to my getInfoClass the reason I have done this is because there are dozens and dozens of different calls in RequestClass all doing different things, this particular request brings back alot of data I have to sort into correct object types so have created this class to do that for me.

anyway inside getInfoClass I sort everything into their correct types etc and pass this data back to secondToLastViewController in a method called recivedData, this is also where I think things are going wrong... as I create a new instance of secondToLastViewController the thing is I dont know how to pass the data back to the same secondToLastViewController that is already on the stack and was where the original request came from.

- (void) recivedData {
// do some stuff then pass data back to secondToLastViewController

SecondToLastViewController *sec = [[SecondToLastViewController alloc] init];

    [sec sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5];

}

Now going back into SecondToLastViewController the thread lands in this method

- (void)sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5{

    // call detailed view onto the stack
    lastViewController *last = [[lastViewController alloc] initWithNibName:@"lastViewController" bundle:nil];

    [self.navigationController pushViewController:last animated:YES];
}

after the thread reaches this point nothing happens... all the data is there and ready to be sent but the new view is never pushed to the controller stack.. and I think it is due to me declaring another version of secondToLastViewController when I am inside getInfoClass

what I would like to know firstly is how do I pass the recived data in sendGetSeriesArrays to the final view and secondly how do i even load the lastview onto the navigation stack?

4

2 回答 2

1

您的观察是正确的,您正在 getInfoClass 中再次创建 secondToLastViewController 实例。不要这样做,您必须使用委托/协议方法将数据传递回 secondToLastViewController。

这样做
在 getInfo 类中定义一个协议

获取信息类.h

@protocol GetInfoClassProtocol <NSObject>

 //delegate method calling after getting data
 // I dont know the argument types give it properly 
 - (void)sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5; 

@end  

// declare the delegate property 

@property (assign, nonatomic)id<GetInfoClassProtocol>delegate;

获取信息类.m

- (void) recivedData {
// do some stuff then pass data back to secondToLastViewController
    if ([self.delegate respondsToSelector:@selector(sendGetSeriesArrays: param2:)]) 
  {
     [self.delegate sendGetSeriesArrays:pram1 Pram2:pram2 Pram3:pram3 Pram4:pram4 Pram5:pram5];
  } 

}

secondToLastViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 //..
 RequestClass.delegate = self;
 [RequestClass Getinfo:storedInfoPram];
}

您的 secondToLastViewController 应符合GetInfoClassProtocol

于 2013-05-15T04:30:40.583 回答
0

有很多方法可以做到这一点。在您的revivedData函数中,您可以:

1) 维护一个指向导航控制器的指针getInfoClass,然后您可以从导航堆栈上的视图控制器中获取最后一个视图控制器并使用它。这将是视图控制器的活动实例。有一些方法可以从窗口对象中恢复它,但这些方法看起来很脆弱,我不推荐这种方法。

2)您可以将指向 self 的指针传递secondToLastViewController给您的RequestClass getInfo呼叫,然后按住它并将其传回。这可能会很痛苦,具体取决于您已经拥有的代码量。

3)如果您永远不会拥有一个以上的类,则可以维护该类的静态实例secondToLastViewController。请参阅如何在 Objective-C 中声明类级属性?

于 2013-05-15T04:33:58.483 回答