2014-03-24 51 views
1

每當我點擊按鈕flipButton一次,變量curPage添加4.我想增加一個。有人能告訴我什麼是錯的嗎? 感謝爲什麼我不能逐個遞增變量?

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    curPage = 0 ; 
    NSString * dic = nil; 
    if ((NSInteger)tempIndex == 0) { 
     type = @"Fire_"; 
     dic = [NSString stringWithFormat:@"%@%d",type,0]; 
    } 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:dic ofType:@"htm"]; 
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    [contentWebView loadHTMLString:htmlString baseURL:nil]; 
    UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Flip" 
            style:UIBarButtonItemStyleBordered 
            target:self 
            action:@selector(flipView)]; 
    self.navigationItem.rightBarButtonItem = flipButton; 
} 

-(IBAction)flipView{ 
    curPage = curPage + 1; 
    NSString *path = [NSString stringWithFormat:@"%@%d",type,(int)curPage]; 
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:path ofType:@"htm"]; 
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
    [contentWebView loadHTMLString:htmlString baseURL:nil]; 
    NSLog(path); 
} 
+0

curPage的數據類型是什麼?我看你把它投給了一個int。你不應該那樣做。 –

+0

另外,請嘗試放置'NSLog(@「%@」,curPage);' 'curPage = curPage + 1;' –

+0

後面的flipView函數應該是IBAction類型嗎?我認爲IBAction是爲故事板設計的。但是,在上面的代碼中,您正在將flipView與您以編程方式創建的按鈕相關聯 – Max

回答

2

我的猜測是,這是因爲你聲明時curPage把星號的int後:

int *curPage; 

這也是爲什麼由編譯器在你的curPage前面添加(int)堅持的原因當它傳遞到stringWithFormat:

這聲明瞭一個整數指針,而不是一個整數。指針按照指向的大小遞增,因此在具有32位的平臺上,它們增加四(32位所採用的字節數)。聲明原始類型的變量時,不需要星號。

相關問題