2013-04-10 70 views
1

我不知道如何弄清視圖控制器1中視圖控制器2的滑塊值的變化。我想我正確調用它,但值不傳遞給視圖。如何使用viewcontroller 2中的滑塊進行更改,以反映viewcontroller 1中的更改​​?

我把一個滑塊放在nib文件中,當我改變它的值時,矩形的高度和寬度的值應該改變。

這裏是我的//appdelegate.m

CGRect bounds  = [self.window bounds]; 
KaleidoTab *view = [[KaleidoTab alloc] initWithFrame:bounds]; 
view.backgroundColor = [UIColor whiteColor]; 



KaleidoTabFirstViewController *vc = [[KaleidoTabFirstViewController alloc] init]; 
[vc setView: view]; 

KaleidoTabSecondViewController *vc2 = [[KaleidoTabSecondViewController alloc] init]; 
//[vc2 setView: view]; 
vc2.vc = vc; 

self.tabBarController = [[UITabBarController alloc] init]; 
self.tabBarController.viewControllers = @[vc, vc2]; 
self.window.rootViewController = self.tabBarController; 

這裏是我的//secondviewcontroller.h

#import <UIKit/UIKit.h> 
#import "KaleidoTabFirstViewController.h" 
@interface KaleidoTabSecondViewController : UIViewController { 
IBOutlet UISlider *changeSize; 
IBOutlet UILabel *label1; } 
@property KaleidoTabFirstViewController *vc; 
@property (nonatomic, retain) IBOutlet UISlider *changeSize; 
- (IBAction) changeSizeSlider:(id)sender; 

這裏的//secondviewcontroller.m

- (IBAction)changeSizeSlider:(UISlider *)sender 
{ 
/// Change label to match slider's value 
label1.text = [NSString stringWithFormat:@"%g", changeSize.value]; 
CGFloat changeSizeCont = changeSize.value; 
((KaleidoTab *)vc.view).rect_width = changeSizeCont; 
((KaleidoTab *)vc.view).rect_height = changeSizeCont; 
} 

kaleidotab.m有繪製矩形的方法。

我合成的屬性,一切都很好。我認爲我的firstviewcontroller對象有問題。

感謝您的時間。 謝謝

+0

這非常令人費解。爲什麼要創建視圖並將其分配給視圖控制器?我會定義視圖控制器使用(作爲它的視圖屬性,在筆尖)的KaleidoTab視圖。然後,使用標準的MVC實踐來定義一個數據模型,該數據模型保存在滑塊上操作的值。任何希望在數據模型更新時都可以更新的視圖。 – 2013-04-11 00:08:21

+0

@MattMartel我相信我正確的方式,與選項卡式應用程序模板我可以讓我的viewcontrollers每當我想要編程方式調用,但問題是與具有nib文件的secondviewcontroller。該值不是從滑塊的cahngevalue傳遞到視圖對象vc2。試圖弄清楚,但沒有成功。 – user2038249 2013-04-11 00:28:51

+0

滑塊標籤是否正確更新? – Matt 2013-04-11 00:42:46

回答

0

您的代碼使用Java類語法而不是Objective-C語法。你會發現,如果你採用的Objective-C的OOP語法簡化診斷這樣的問題:

// For getting property values 
[objectName propertyName]; 

// For setting property values 
[objectName setPropertyName]; 

很多iOS的框架是如何工作取決於以下約定。嘗試將您的代碼更改爲以下內容:

- (IBAction)changeSizeSlider:(UISlider *)sender 
    { 
    // Change label to match slider's value 
    [label1 text] = [NSString stringWithFormat:@"%g", [changeSize value]]; 

    // Var for changing size 
    CGFloat changeSizeCont = [changeSize value]; 

    // Log to the console so that we can confirm changeSizeCont is being set appropriately 
    NSLog(@"changeSizeCont = %d", changeSizeCont); 


    /* I'm not used to this current syntax, this may be what's going wrong 
    It's confusing to read, it should be clearer. 
    I understand that you are attempting to grab the 1st view controller and 
    change its view properties, I think you can drop the 
    ((KaleidoTab *)vc.view).rect_width and replace it with 
    vc.view.rect_width 
    vc.view.rect_height 
    */ 
    ((KaleidoTab *)vc.view).rect_width = changeSizeCont; 
    ((KaleidoTab *)vc.view).rect_height = changeSizeCont; 

    // Log to the console so that we can confirm the rect_width and rect_height properties are receiving the change 
    NSLog("Current values for VC's view properties:"); 
    NSLog(@"rect_width = %d", ((KaleidoTab *)vc.view).rect_width); 
    NSLog(@"rect_width = %d", ((KaleidoTab *)vc.view).rect_height); 

    } 

這應該有助於您朝正確的方向前進。您將能夠在控制檯上檢查屬性值是否正在更新。如果它們並且視圖沒有適當地改變,那麼您可能需要查看應用程序的生命週期,並找出需要重新加載或更新視圖的位置,以反映視圖在屏幕上的變化。

+0

請確保您閱讀了大量評論,我認爲這是您可能犯錯的地方。 – Matt 2013-04-11 01:11:53

+0

我很感謝你的時間。我可以在日誌控制檯中看到滑塊的值已更改。但是當我切換到firstviewcontroller選項卡視圖繼續其默認值。更改後的值可能會發送到視圖,但是,我認爲它必須重新加載才能使新的更改生效。 – user2038249 2013-04-11 02:15:55

+0

雖然我很欣賞你的時間。 – user2038249 2013-04-11 02:16:40

相關問題