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?

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

2 Answers2

1

Your observation is correct you are creating the secondToLastViewController instance again inside the getInfoClass. Dont do like that you have to use delegate/protocol approach for passing the data back to the secondToLastViewController.

Do like this
Define a protocol in getInfo class

getInfoClass.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;

getInfoClass.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];
}

Your secondToLastViewController should conform to the GetInfoClassProtocol

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • ahh crap.. I have had so much trouble with delegate/protocols... going to have to go through another tutorial or something.. damnit. – HurkNburkS May 15 '13 at 04:31
  • No dude.. Its simple.. give some time i will explain – Anil Varghese May 15 '13 at 04:33
  • Even if you create a delegate, you will need to maintain a reference to the object which implements the protocol. This could be some work. The place where a delegate is really helpful is if there are several classes which may need to implement it for different purposes. – Adam Shiemke May 15 '13 at 04:35
  • @Anil yes I am going to do it.. but will have to be tomorrow as I have to take my cat to the vet now.. so will come back to this tomorrow.. thank you very much for your suggestion.. Im just going to have to do it :P – HurkNburkS May 15 '13 at 04:38
  • Ok. I have updated my answer.. Try to implement if you get any doubt let me know – Anil Varghese May 15 '13 at 04:49
  • awesome i think maybe you might have an error in your last code segment.. is it supposed to be GetInfoClass.delegate insdead of the RequestClass.delegate? – HurkNburkS May 15 '13 at 23:11
  • I hope `RequestClass` is the instance of GetInfoClass.. right?? So it should be RequestClass.delegate only. we declared the property in `delegate` in GetInfoClass. `RequestClass.delegate = self` means we are assigning instance of secondToLastViewController to it. So you can call any method in secondToLastViewController. – Anil Varghese May 16 '13 at 03:52
0

There are lots of ways you can accomplish this. In your revivedData function, instead of creating a new instance, you could:

1) Maintain a pointer to the navigation controller in getInfoClass, then you can get the last view controller from the view controllers on the navigation stack and use that. This will be the active instance of the view controller. There are ways to recover this from the window object, but those seem fragile and I would not recommend that approach.

2) You can pass a pointer to self from secondToLastViewController to your RequestClass getInfo call, then hold on to that and pass it back. This is probably a pain depending on the amount of code you have already.

3) You can maintain a static instance of the class if you will never have more than one secondToLastViewController. See How do I declare class-level properties in Objective-C?

Community
  • 1
  • 1
Adam Shiemke
  • 3,734
  • 2
  • 22
  • 23