我启用了 ARC,所以我不确定为什么我的引用为空。
我的视图控制器在视图加载后立即实例化 UIView '<strong>theGrid'。
后来我在另一个调用 UIViewContoller - (void) updateTheGrid:(id)sender
方法的类(MyOtherClass)中切换,该方法根据 NSLog 调用,但是当我输出 UIView 以查看它是否存在时,它返回 null。
我究竟做错了什么?我的印象是 ARC 跟上一切。我觉得我的麻烦来自 mm "MyOtherClass", ViewController * vc = [[ViewController alloc] init];
因为我觉得那只是创建一个新实例。但如果是这样的话,我想如何引用旧实例并调用该方法?
NSLOG 输出
[28853:c07] Intial Grid: <GridView: 0x8e423b0; frame = (0 0; 768 1024); layer = <CALayer: 0x8e43780>>
[28853:c07] Update The Grid (null)
GridView.h
#import <UIKit/UIKit.h>
@interface GridView : UIView
- (void) gridUpdated;
@end
网格视图.m
#import "GridView.h"
@implementation GridView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(@"initWithFrame");
}
return self;
}
- (void)drawRect:(CGRect)rect{
NSLog(@"Grid Draw Rect");
}
- (void) gridUpdated {
NSLog(@"GRID VIEW.m : Grid update called");
[self setNeedsDisplay];
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
#import "GridView.h"
@interface ViewController : UIViewController {
GridView *theGrid;
}
@property (strong, retain) GridView * theGrid;
- (void) updateTheGrid : (id) sender;
@end
视图控制器.m
#import "ViewController.h"
#import "GridView.h"
@interface ViewController () {}
@end
@implementation ViewController
@synthesize theGrid;
- (void)viewDidLoad {
[super viewDidLoad];
//draw the grid
theGrid = [[GridView alloc] initWithFrame:self.view.frame];
NSLog(@"Intial Grid: %@", theGrid);
[self.view addSubview:theGrid];
}
- (void) updateTheGrid : (id) sender{
NSLog(@"Update The Grid %@", theGrid);
[theGrid gridUpdated];
}
@end
我的其他类.m
- (void) mySwitch : (id) sender {
ViewController * vc = [[ViewController alloc] init];
[vc updateTheGrid:sender];
}