2013-10-05 27 views
2

我明白(我認爲)爲什麼這不起作用,但我不知道如何讓它工作。如何發送方法來查看來自UIButton調用的操作

我有一個方法,我在viewWillAppear調用像這樣(我已經簡化了這一問題的緣故代碼):

- (void)viewWillAppear:(BOOL)animated 
{ 
     [self displayHour:0]; 
} 

然後我有一個小的循環,建立在viewDidLoad一些按鈕:

NSUInteger b = 0; 

for (UIButton *hourButton in hourButtons) { 
    [hourButton setTag:b]; 
    [hourButton setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)b] forState:UIControlStateNormal]; 
    [hourButton addTarget:self action:@selector(showHour:) forControlEvents:UIControlEventTouchUpInside]; 

    b++; 
} 

那麼對於動作我有這樣的:

- (IBAction)showHour:(id)sender 
{ 
     // Logs 0, 1, etc. depending on which button is tapped 
    NSLog(@"Button tag is: %lu", (long int)[sender tag]); 

    // This currently throws this error: 
    // No visible @interface for 'UIView' declares the selector 'displayHour:' 

    // What I want to do is be able to call this method on 
    // the main view and pass along the button tag of the 
    // button that was tapped. 
    [mainView displayHour:(long int)[sender tag]]; 
} 

問題是如何從showHour:操作中調用displayHour的主視圖?

如果你需要它時,displayHour方法看起來像這樣(簡化,基本上更新主視圖中的某些標籤和圖像視圖):

- (void)displayHour:(NSUInteger)i 
{  
    // The temperature 
    hourTemp = [[hourly objectAtIndex:i] valueForKeyPath:@"temperature"]; 

    // The icon 
    hourIcon = [[hourly objectAtIndex:i] valueForKeyPath:@"icon"]; 

    // The precipitation 
    hourPrecip = [[hourly objectAtIndex:i] valueForKeyPath:@"precipProbability"]; 

    // SET DISPLAY 
    [hourTempField setText:hourTemp]; 
    [hourIconFrame setImage:hourIcon]; 
    [hourPrecipField setText:hourPrecip]; 
} 

謝謝!

+0

什麼是MAINVIEW?如果這是指向相同的對象,然後使用自我。如果它是任何其他階級的對象而不是其罰款。我覺得方法是在同一個班級。所以請使用自己的方法 –

回答

1

而不是

[mainView displayHour:(long int)[sender tag]]; 

做:

[self displayHour:(long int)[sender tag]]; 
+0

就是這樣!謝謝。請參閱@ C_X對原始問題的評論。 –

相關問題