1

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

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

感谢您的时间。谢谢

4

1 回答 1

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);

    }

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

于 2013-04-11T01:03:30.967 回答