簡體   English   中英

錯誤:“ createCharacter”之前的預期表達式

[英]error: expected expression before 'createCharacter'

所以我正在使用presentmodalviewcontroller更改ipad應用程序中的活動視圖。 但是,當我嘗試使用語句[self presentModalViewController:createCharacter animated:NO];更改它時[self presentModalViewController:createCharacter animated:NO]; 在由按鈕觸發的動作中。 但是我得到一個錯誤,說'createCharacter'之前期望的表達式。 createCharacter是我創建的自定義視圖控制器。有人知道我在做什么錯嗎? 如果您需要更多相關代碼,請告訴我,謝謝

其他相關代碼:

#import "createCharacter.h";


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

createCharacter.h:

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

@interface createCharacter : UIViewController {
    IBOutlet UIView *view;
}

@end

我很想看看一些代碼,如果沒有它,也許這個建議是錯誤的,但是...根據我的經驗,我一直將IBActions與單個參數一起使用,並且該參數一直是發送者,所以像是將按鈕按下按鈕

-(IBAction) presentNewController:(id)sender` 

發送者是被按下的按鈕。

如果您使用類似的方法來檢測IB中的按鈕按下,那么在代碼中您會想要的是:

// 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];
    }
}

您需要先分配和初始化createCharacter,然后才能將其推入視圖。

假設createCharacter是一個視圖控制器:

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

看來您正在將一個類發送到presentModalViewController:animated:。 您需要按照Rog所示初始化類。 至於聖雄的回答,我不知道為什么要擺脫你的錯誤。 對於OS X,他是正確的,但對於iOS,則不需要此參數。

遵循Rog的示例后,仍然有一些原因可能仍然導致錯誤:

  1. 您有一個名為createCharacter的變量,這意味着它已被初始化。 在這種情況下,將類的名稱更改為CreateCharacter。 通常將類大寫,這樣可以確保它不會與變量混淆。
  2. createCharacter頭文件未正確導入。 如果您在XCode中更改文件的名稱,則實際上不會更改文件的名稱。 如果您嘗試使用新名稱導入文件,它將無法正常工作。
  3. 您的初始化代碼不正確。 在Rog的示例中,他使用了默認參數名稱。 您應該將yourNibNameORnil更改為NIB文件的名稱,如果不使用NIB, yourNibNameORnil其更改為nil。 假設NIB位於應用程序包中,您還應該將yourBundleNameORnil更改為nil。 這是一個示例,假設NIB位於應用程序中,並且名為createCharacter.xib:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM