views:

103

answers:

1

Hi, I have a small doubt. I have a NSObject class where I am trying to display an alert view. So after the alert view is displayed when I tap on OK button I want to push a navigation controller onto the stack. Can I push a navigation controller from general NSObject class? Please let me know guys..thanks for your time..

This is the code..

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{    
  SettingsViewController *homeView = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
    [self.navigationController pushViewController:homeView animated:NO];
    [homeView release];

}

I am creating a property called navigationController of type UINavigationController and when I catch the error I am displaying an alert view and I am using above method to push the view controller but it doesn't work..

+2  A: 

Yes and no... depending on how you have your application set up. To push views onto the navigation stack you need to have a navigation controller.

Does your NSObject have access to this navigation controller - you might have to set up a delegate method that gets called from your delegate view when the alert view delegate gets called in your NSObject.

I'm just wondering why you're displaying a UIAlertView in an NSObject, why aren't you displaying it in a UIView or a UIViewController?

CustomObject.h

@protocol CustomObjectDelegate<NSObject>
@optional
- (void)customObjectAlertViewDidClickOk;
@end

@interface CustomObject : NSObject <UIAlertViewDelegate>{
    id<CustomObjectDelegate> delegate;
}
@property (nonatomic, assign) id<CustomObjectDelegate> delegate;
@end;

CustomObject.m

@synthesize delegate;
// then put this:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    [delegate customObjectAlertViewDidClickOk];
}

Then your ViewController .h file needs to include the custom object and assign the delegate methods:

#include "CustomObject.h"
@interface MyViewController : UIViewController <CustomObjectDelegate> {

}
@end

and the .m viewDidLoad (or similar):

- (void)viewDidLoad{
    CustomObject *obj = [[CustomObject alloc] init];
    [obj setDelegate:self];
}
- (void)customObjectAlertViewDidClickOk{
    AnotherViewController *page = [[AnotherViewController alloc] initWithNibName:nil bundles:nil];
    [self.navigationController pushViewController:page];
}

Thats how I would do it - given I'm not too sure i understand quite what you're asking. :) thats all off the top of my head as well - so don't take it letter for letter, but you have the basis there to start off with. You can build on it. Look up @protocols and delegate methods, its all in there. :)

Thomas Clayson
thanks for the reply thomas..I am using a service and the service is an NSObject class..so whenever I encounter errors in the service I am displaying alert views..I used alertview's clickedButtonAtIndex method in NSObject class but I am not sure how to push the navigation controller from this method..can you please help me regarding this..if possible can you please post a sample code..
racharambola
i'm not to sure what you really mean. You might have to post up some code yourself for me to help you. However I would suggest you just create a delegate method that gts called in `clickedButtonAtIndex`. I will update my post with an example...
Thomas Clayson
thanks thomas..I will give a try..I included some code..please have a look at it if you have some time..once again thanks for your time
racharambola