开发者

error: expected expression before 'createCharacter'

开发者 https://www.devze.com 2023-01-29 08:20 出处:网络
so i am using presentmodalviewcontroller to change the active views in an ipad app. however when i try to change it using the statement [self presentModalViewController:createCharacter animated:NO]; i

so i am using presentmodalviewcontroller to change the active views in an ipad app. however when i try to change it using the statement [self presentModalViewController:createCharacter animated:NO]; in an ibaction that is triggered by a button. however i get an error saying expected expression before 'createCharacter'. createCharacter is a custom view controller that i have created... does anyone know what i am doing wrong? if you need any more relevant code just let me know,thanks

additional relevant code:

#import "createCharacter.h";


-(IBAction) buildCharacter{
    [self presentModalViewController:createCharacter animated:NO];      
}

createCharacter.h :

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface create开发者_如何学JAVACharacter : UIViewController {
    IBOutlet UIView *view;
}

@end


I'd love to see some code to look at, and without it maybe this advice will be wrong, BUT...in my experience I've always used IBActions with a single argument, and that argument has always been the sender, so something like tying a button press to `

-(IBAction) presentNewController:(id)sender` 

where sender is the button that was pressed.

If you use a method like that to detect the button press from IB, then in code what you would want is something like:

// In your current view controller, the target where you wired up the button
-(IBAction) presentNewController:(id)sender
{
    if([sender isEqual:<whatever button you expect>])
    {
        CustomController *con = [[[CustomController alloc] init] autorelease];
        [selfpresentModalViewController:con animated:YES];
    }
}


You need to allocate and initialise createCharacter before you can push it into the view.

Assuming createCharacter is a view controller:

createCharacter *customView = [[createCharacter alloc] initWithNibName:yourNibNameORnil bundle:yourBundleNameORnil];
[self presentModalViewController:customView animated:YES];
[customView release];


It appears that you are sending a class to presentModalViewController:animated:. You need to initialize the class as Rog showed. As for MahatmaManic's answer, I have no idea why that was getting rid of your error. He is correct for OS X, but the argument is not required for iOS.

There are a few reasons you could still be getting the error after following Rog's example:

  1. You have a variable named createCharacter, which means it was already initialized. In this case, change the name of your class to CreateCharacter. Classes are usually capitalized, and this would ensure that it is not confused with a variable.
  2. The createCharacter header file is not properly imported. If you change the name of the file in XCode, it does not actually change the name of the file. If you try importing the file using the new name, it will not work.
  3. Your initialization code wasn't right. In Rog's example, he used the default argument names. You should change yourNibNameORnil to the name of your NIB file, or nil if you are not using a NIB. You should also change yourBundleNameORnil to nil, assuming the NIB is located in your application's bundle. Here is an example assuming the NIB is in the application and named createCharacter.xib:

    createCharacter *customView = [[createCharacter alloc] initWithNibName:@"createCharacter" bundle:nil];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号