0

i have a application which is handling lots of functions and also having lots of navigations.

I am using lots of BOOLs inside my application. I know its not efficient. So I want to remve these BOOLs to create more efficient and clean application.

This is my question. For a Example lets say I have 3 UIViewControllers.

Test1 , Test2, Test3

I need to navigate to Test3 from both Test1 and Test2 view Controllers . Also if I navigate to Test3 View Controller from Test1, I need to execute one method and, if I navigate to Test3 from Test2, I need to Execute another method.

Currently what i am doing is, I am Using Globals.h and Globals.m classes to over come this problem. I create a BOOL in Globals and enable that BOOL value in Test1 and, I checked that BOOL value inside Test3 and execute the method i want.

This is just an example. I got lots of view controllers and lots of behaviors for the application. So I create lots of BOOLs inside Globals and used them in different Classes. So this is a pain to handle lots of bools in one application and it not good too. So can any one please help me how to overcome this problem.

Thanks in advance :)

Sameera Chathuranga
  • 3,638
  • 3
  • 27
  • 48

3 Answers3

1

you can also check class with NSObject method isKindOfClass.

You can also find example given in that method explaination.

Here you can compare the object is which kind of class, and accordingly you can perform your operation.

For that you can pass your self reference to Controller3 every time and you can store it with id type.

Hope this will help you in your code.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
1

Try using the viewControllers property of the navigation controller.

UINavigationController reference

What I mean is, when a view controller loads that you need a specific action done based on where it came from grab the view controllers array and look at the object at location n - 2 (where n is the number of elements in the array). Then test the type of class of that object using isKindOfClass method and perform the appropriate action.

Rough Example:

-(void)viewDidLoad {

 [super viewDidLoad];

 NSArray *viewControllers = [[self navigationController] viewControllers];

 int parentIndex = [viewControllers count] - 2;
 UIViewController *parentVc = [viewControllers objectAtIndex:parentIndex];

 if ([parentVc isKindOfClass:ClassA.class]) {
  //action
 }
 else if ([parentVc isKindOfClass:ClassB.class]) {
  //different action
 }
 else ... etc

}

Mike
  • 2,399
  • 1
  • 20
  • 26
  • Thanks Guys ... Great Help ...all the answers are really helpful for me.. but mrunal is the one who gave the answer first .. so i ll tick it. But honestly all the answers are really really helpful thanks :) – Sameera Chathuranga May 21 '12 at 18:14
1

Like @mrunal said you can use isKindOfClass. just figured I through in some code.

// self is Test3

if ([self.presentingViewController isKindOfClass:[Test1ViewController class]]) {
    // Run your method for Test1 - Test 3 here.
}

If you're pushing or presenting a modal you'll need to grab the actual viewController since presentingViewController will be a UINavigationController. This is how I do it.

// self is Test3

if ([self.presentingViewController.childViewControllers.lastObject isKindOfClass:[Test1ViewController class]]) {
    // Run your method for Test1 - Test 3 here.
}
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • 1
    Thanks Guys ... Great Help ...all the answers are really helpful for me.. but mrunal is the one who gave the answer first .. so i ll tick it. But honestly all the answers are really really helpful thanks :) – Sameera Chathuranga May 21 '12 at 18:13
  • Yep he deserves it :) glad we could all help. – Ryan Poolos May 21 '12 at 18:15
  • hey dude ... I got one question ... what if I need to access a method in Test1ViewController ? In here its checking for whole class not a particular method :) .. may be its a silly question to ask .. but i just got it in my mind – Sameera Chathuranga May 21 '12 at 18:27
  • 1
    You can use [NSObject respondsToSelector:@selector(myMethod)] if it returns yes then you'll know the method is defined. That what you are talking about? – Ryan Poolos May 21 '12 at 19:17