1

我试图在第二个视图控制器中调用我的视图,并在第一个视图控制器中更改一组新值。我正在使用指针来获取我的第一个 viewcontroller 视图(使用指向 firstview 控制器的属性),但我没有看到视图中的任何更改。想法表示赞赏。谢谢

//secondviewcontroller.m

#import "SecondViewController.h"
#import "FirstViewController.h"
#import "Tab.h"
#import "AppDelegate.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize changeSizeSlider;




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"second", @"second");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];

    }
    return self;
}


- (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];


    NSLog(@"changeSizeCont = %f", changeSizeCont);


    ((Tab *)vc.view).rect_width  = changeSizeCont;
    ((Tab *)vc.view).rect_height = changeSizeCont;

    // The values here are displayed but i think the view is not reloaded with the nw values
    NSLog(@"Current values for VC's view properties:");
    NSLog(@"rect_width = %f", ((Tab *)vc.view).rect_width);
    NSLog(@"rect_height = %f", ((Tab *)vc.view).rect_height);

}

通过调用 //secondviewcontroller.h 中的属性调用 firstviewcontroller 的视图对象 vc

@property FirstViewController *vc;

//firstviewcontroller.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController


- (void)viewWillAppear:(BOOL)animated
{
    //NSLog(@">>> %@", NSStringFromSelector(_cmd) );
    self.title = NSLocalizedString(@"First", @"First");
    self.tabBarItem.image = [UIImage imageNamed:@"first"];
    // Get timer
    NSTimer *timer = [NSTimer timerWithTimeInterval: 0.5
                                                      target: self.view
                                                    selector: @selector(setNeedsDisplay)
                                                    userInfo: nil
                                                     repeats: YES];

    // Get runloop, add timer to runloop
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode: NSDefaultRunLoopMode];


}

我有绘制类中编写的矩形的函数。

//appdelegate.m #import "TabAppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "Tab.h"

@implementation TabAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    time_t seed = time(0);
    srandom((int)seed);

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

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

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


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

    [self.window makeKeyAndVisible];
    return YES;
}

Tab.m 具有将出现在 firstview 中的绘图功能,而 secondview 具有处理 firstviewcontroller 中的更改的 nib。

我几乎认为我在任何地方都没有错,我花了将近一个星期没有弄清楚这个问题。感谢你们的时间。

4

2 回答 2

3

在其他视图中处理更改、操作或用户交互是委托的基本用例。最佳实践是让第一个视图控制器成为第二个视图控制器的委托,然后当第二个视图控制器中发生事件时,它会调用其委托上的某些方法来通知它。

保持视图之间的值/状态同步的其他选项是:

于 2013-04-12T18:53:07.330 回答
0

您还可以在第二个视图控制器上创建一个块属性,第一个视图控制器在将其推送到堆栈时设置该属性

typdef void(^CallWhenChanged)(CGSize);
@interface ViewController2

@property(strong, nonatomic) CallWhenChanged callBlock;

@end

然后:

- (IBAction)changeSizeSlider:(UISlider *)sender
{
    //do stuff
    if( self.callBlock != nil )
    {
        self.callBlock(CGSizeMake(rect_width,rect_height));
    }
}

jszumski 提到的委托是另一种选择,但我更喜欢在处理多个委托方法时使用委托。

于 2013-04-12T20:41:16.503 回答