如何使用viewcontroller 2中的滑块进行更改,以反映viewcontroller 1中的更改​​?

时间:2013-04-10 23:35:31

标签: iphone ios objective-c uiviewcontroller uislider

我不知道如何用视图控制器1中的视图controller2来计算滑块值的变化。我想我正确地调用它但是值没有传递给视图。

我在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对象出了问题。

欣赏你的时间。 感谢

1 个答案:

答案 0 :(得分: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);

    }

这可以帮助你朝着正确的方向前进。您将能够在控制台上检查属性值是否正在更新。如果它们是并且视图没有正确更改,您可能需要查看应用程序的生命周期并找出需要重新加载或更新视图的位置,以反映视图对屏幕的更改。