我试图在第二个视图控制器中调用我的视图,并在第一个视图控制器中更改一组新值。 我正在使用指针获取我的第一个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);
}
通过调用其属性来调用firstviewcontroller的视图对象vc //secondviewcontroller.h
@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。
我几乎认为我在任何地方都没有错,我花了差不多一个星期没有弄清楚这个问题。 感谢你的时间。
答案 0 :(得分:3)
处理其他视图中的更改,操作或用户交互是delegates的基本用例。最佳实践是让第一个视图控制器成为第二个视图控制器的委托,然后当事件发生在第二个视图控制器中时,它会调用其委托上的某些方法来通知它。
在视图之间保持值/状态同步的其他选项是:
NSNotification
通过NSNotificationCenter
发送 - 最适合用于操作答案 1 :(得分: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提到的代理是另一种选择,但我更喜欢在处理多个委托方法时使用委托。