Prasen
Prasen

Reputation: 81

How to call function from one viewcontroller to another controller?

SettingsStore.h

@interface SettingsStore : IASKAbstractSettingsStore    
{      
    @public     
    NSDictionary *dict;     
    NSDictionary *changedDict;    
}    

- (void)removeAccount;    
@end

menuView.m

-(IBAction)onSignOutClick:(id)sender    
{        
    SettingsStore *foo = [[SettingsStore alloc]init];    
    [foo removeAccount];    
    [self.navigationController pushViewController:foo animated:YES];       
    exit(0);
}

I want to call this removeAccount function from menuView.m. But I am getting error. enter image description here

How to fix it and call this removeAccount.

Upvotes: 0

Views: 201

Answers (2)

Ganesh Bavaskar
Ganesh Bavaskar

Reputation: 764

There are few mistakes in your Code please find them below.

  1. [foo removeAccount]; Calling this method is correct
  2. [self.navigationController pushViewController:foo animated:YES];
    Not correct because SettingsStore is not subclass of UIViewController only subclass of UIViewController can be pushed to Navigation controller
  3. exit(0); Calling this method is not recommended by Apple

Upvotes: 3

Johnny Rockex
Johnny Rockex

Reputation: 4196

You are calling removeAccount correctly from your menuView.m file, but there are several issues with your code:

  1. You are treating foo as though it were a UIViewController, and it's actually a member of the SettingStore class. Does the SettingStore class refer to an actual screen, or is it more a data object (for storing settings?). If it's the latter, you don't want to push it on. You can create it, and use it, but the user doesn't need to see it.

  2. You are calling exit(0); you can remove that line. If you want to remove the menuView.m file from your memory, remove references to it (e.g. from its parent view controller).

  3. The menuView.m file is confusing, as in, is it a view or a viewController. An IBAction I would normally stick in a ViewController file, rather than a view file. Your basic design pattern is MVC (Model / View / Controller). In this case, it seems your SettingStore file is a Model (data), the menuView.m is a View and your code is for the Controller bit.

Upvotes: 0

Related Questions