0

I am trying to pop all the views from my navigation stack if their is an error. the error code is in a NSObject class so I dont have access to the navigationcontroller. THis is the code I have tried

[self.navigationController popToRootViewControllerAnimated:FALSE];

I would like to know the best way to access and pop views from the stack from a NSobject class.

any help would be greatly appreciated.

HurkNburkS
  • 5,332
  • 19
  • 93
  • 176
  • have you tried the Unwind segue? – CaptJak Aug 12 '13 at 01:55
  • no I havent, I have never even heard of this.. lol, One thing you may need to know is that I could be on one of several different views when this error occurs and need to make it back to the root view controller at that point. – HurkNburkS Aug 12 '13 at 02:00
  • not sure if this will work, but possibly send a NSNotification from NSObject, to a class with access to the navigationController. – khanh.tran.vinh Aug 12 '13 at 02:10
  • But why call it from an NSObject Class? – CaptJak Aug 12 '13 at 02:22
  • because its an error class which presents the user with a UIAlertView. I would not want to write the same lot of errors into every viewcontroller to handle the errors.. its just cleaner this way. – HurkNburkS Aug 12 '13 at 03:07

1 Answers1

0

So, you do want the Unwind segue. Simple tool, very useful and easy to implement. This segue will successfully take you back to a ViewController, no matter how deep you are into your hierarchy.

I think that the best way to get to know how to implement this is described in this answer, and also in this answer (which I prefer).

That should work for you.

Community
  • 1
  • 1
CaptJak
  • 3,542
  • 1
  • 25
  • 48
  • okay perfect thankyou.. i am going to try doing it after I have updated xcode as I cannot see that exit button thing. – HurkNburkS Aug 12 '13 at 02:24
  • I am not sure if this is going to work because I am notusing a story board? – HurkNburkS Aug 12 '13 at 02:29
  • @HurkNburkS Bah! Humbug! I should have checked... What you could do is create a custom method in the latest VC with the `popToRootViewControllerAnimated` method, and then call it from another class using [this answer](http://stackoverflow.com/a/9731162/2535467). – CaptJak Aug 12 '13 at 02:46
  • sorry I should have explained better :P man not sure if this is going to work like I had planned.... – HurkNburkS Aug 12 '13 at 02:56
  • Maybe, did you try my suggestion in the comment? – CaptJak Aug 12 '13 at 02:57
  • No, but what I did try way putting a method in the view above the rootviewcontroller for me to call which had the following line of code ** [self.navigationController popToRootViewControllerAnimated:YES];** however this dosnt do anything, I guess because I alloc init this view.. so it thinks its the only view on the stack. – HurkNburkS Aug 12 '13 at 03:04
  • okay what I am deciding to do is pass the navigationController instance with the method calls in an additional parameter.. this way it works.. Its just a hassle becuase I have a requestObjectClass and a reasponseObject class so I need to keep track of this parameter through both NSObject classes where *if* there is an error then I pass that value to the errorClass.. and let it do its thing.. thing Is i have dozens of methods in each of these classes so its going to take abit of time :P – HurkNburkS Aug 12 '13 at 03:26
  • Hohoho @HurkNburkS ... bit of a workaround isn't it? why do you call it from the NSObject class anyways? – CaptJak Aug 12 '13 at 03:27