user134611
user134611

Reputation: 776

Calling a function from another UIViewController

I'm a beginner programmmer, this is for xcode - iPhone. although i made alot of my iPhone app but i seem to lack some understanding of how a simple communication might work.

Specially when I've got 2 ViewControllers.

And I wana call one function of a ViewController from another ViewController. Both are under a tabbarController. What I want to achieve is When I'm in ViewA, after tapping on a tableCell, I Should Invoke a method of ViewB and the NavigationBar of ViewB pushes to viewDetail.

The Following is the code i'm using in ViewControllerA.h (where I'm calling a method)

@class ViewControllerB;
@interface SmartDDxViewController : UIViewController {

    IBOutlet UITableView *tableView;
    ViewControllerB *xViewController;


}
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) ViewControllerB *xViewController;

And this is what I use to invoke it..ViewControllerA.m

ViewControllerB *ddViewController = [[ViewControllerB alloc] init];
self.xViewController = ddViewController;    
[xViewController InitialiseDetailWithId:2 title:@"HEYA"];

Heres the InitialiseDetailWithId code: in ViewControllerB.m

-(void)InitialiseDetailWithId:(NSInteger)pkey title:(NSString *)tt{

    NSLog(@"InitialiseDetailC=========================================");
    AppDelegate *appDelegate = (Smart_DifferentialsAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate GetConditionDetailsWithId:pkey];
    DDisViewController *viewController= [[DDisViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    viewController.title = tt;
    [self.NavBar pushViewController:viewController animated:YES];
    //[tt release];
    [viewController release];
    viewController = nil;       
    [self say:@"HEYA"]; //this is ALERTVIEW box that displays HEYA
}

I'm getting all information fine, and the alertview does get displayed. But when I chose that View in TabBar, its not pushed.

Upvotes: 1

Views: 8930

Answers (3)

Tim Kozak
Tim Kozak

Reputation: 4182

//current view controller index
int currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];

//previous view controller (index -1)
AccountViewController *account = (AccountViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1];

(access to anything you want)
account.property = object;
[account doSmthng];

Upvotes: 1

PeyloW
PeyloW

Reputation: 36752

Do not use direct access between view controllers, instead use the delegate pattern. Define your controller like this:

@protocol ViewControllerAInitDelegate;
@interface ViewControllerA : UIViewController {
  IBOutlet UITableView *tableView;
  id<ViewControllerAInitDelegate> initDelegate;
}
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, assign) ViewControllerAInitDelegate *initDelegate;
@end

@protocol ViewControllerInitDelegate
-(void)initializeDetailWithId:(NSInteger)pkey title:(NSString)tt;
@end

So in

Now let your application delegate conform to the ViewControllerInitDelegate protocol. It should look something like this:

@interface AppDelegate : NSObject <UIApplicationDelegate, ViewControllerInitDelegate> {
  IBOutlet UITabBarControler* tabBarController;
  IBOutlet ViewControllerA* controllerA;
  IBOutlet ViewControllerB* controllerB;
}
@end;

The AppDelegate should know about both ViewControllerA, and ViewControllerB, but neither of the view controller should know about each other. This way it will be much easier to debug and extend your app.

Upvotes: 1

mahboudz
mahboudz

Reputation: 39376

In each of your view controllers, you might want to add a instance variable/property to keep track of the other view controller.

you might have for example:

@interface ThisViewController : UIViewController {
    SomeViewController      *sViewController;
// other instance variables
}
@property (nonatomic, retain) SomeViewController        *sViewController;

This not only makes it easier to call methods from the other view controller and access its public properties, but it also allows you an easier way of flipping between the two (with or without animation).

Upvotes: 0

Related Questions