0

ok, this may be a very basic question and I apologise if it is, but its driving me mad and I really need to get a better understanding.

I have an app that I am developing and using a new view controller and xib file for each page.

I call each page by using this code:-

HelpVC = [[[HelpViewController alloc] initWithNibName:@"HelpViewController" bundle: [NSBundle mainBundle]]autorelease];
[self presentModalViewController:HelpVC animated:YES];

Which works great.

However if I have this declared in a file and I then want to call it somewhere else I am getting errors all over the place.

is there any way to initialise all the xib files first in a class file and then call them from that file when needed??

Thanks

This is the error I get back

error: expected specifier-qualifier-list before 'UserInputViewController'

It seems to cause all of my declarations in the .h file to error with the message above.

Some sample code is:-

.h file

#import <UIKit/UIKit.h>
#import "CgePWViewController.h"
#import "LogBackInViewController.h"
#import "LoginViewController.h"

@interface SettingsViewController : UIViewController
{
    CgePWViewController *cPWVC;
    LogBackInViewController *LBIVC;
    LoginViewController *login;
}

@property(nonatomic, retain) CgePWViewController *cPWVC;
@property(nonatomic, retain) LogBackInViewController *LBIVC;
@property(nonatomic, retain) LoginViewController *login;

so basically as soon as I have added in LoginViewController it causes everything else to error, if I take this out then the app runs perfectly.

hanimal_p
  • 303
  • 1
  • 3
  • 13
  • 1
    Please add the code the exact code that gives the error, and also the exact error message, so that we can try and help you. – JosephH Aug 06 '12 at 18:31

2 Answers2

1

Does your LoginViewController.h imports the SettingsViewController as well? It seems to be a "cross reference" (I don't know if this term makes sense in English as it does in Portuguese).

Before your

@interface SettingsViewController : UIViewController

put a

@class LoginViewController;

and check if it helps.

Bruno Koga
  • 3,864
  • 2
  • 34
  • 45
  • Hi, yes there is cross referencing hence why I wanted to maybe load the view controllers at the start of the app and reference them throughout the app. I will try what you suggest. – hanimal_p Aug 06 '12 at 18:42
  • That worked, so should I use @class instead of #import for all the view controllers? – hanimal_p Aug 06 '12 at 18:47
  • [Here](http://stackoverflow.com/questions/322597/class-vs-import), this will help you clarify your doubt. – NSPunk Aug 06 '12 at 18:57
  • funny enough I was just reading that post. Thanks for all your help. – hanimal_p Aug 06 '12 at 19:01
0

Don't initialise your view controllers to put them in properties. Instead, create them as soon as you need them.

Let's assume you want to show a view controller when somebody pushes a button. Add this method to your view controller (let's assume it's named MyFirstViewController):

- (IBAction)someButtonDidPush: (UIButton *)sender {
    MySecondViewController *vc = [[MySecondViewController alloc] init];

    // Do stuff and then show it. One way may be like this:
    [self presentViewController: vc animated: YES completion: nil];
}

Just hook up the button with the method above. No properties needed! You're (probably) done.

Constantino Tsarouhas
  • 6,846
  • 6
  • 43
  • 54