我试图在第二个视图控制器中调用我的视图,并在第一个视图控制器中更改一组新值。我正在使用指针来获取我的第一个 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。
我几乎认为我在任何地方都没有错,我花了将近一个星期没有弄清楚这个问题。感谢你们的时间。