Objective c UIView引用返回已启用的空点弧
我已启用ARC,因此我不确定我的引用为何为空 “我的视图控制器”在加载视图后立即实例化UIView“theGrid” 后来,我在另一个类(MyOtherClass)中进行了切换,该类调用UIViewContollerObjective c UIView引用返回已启用的空点弧,objective-c,uiview,null,automatic-ref-counting,Objective C,Uiview,Null,Automatic Ref Counting,我已启用ARC,因此我不确定我的引用为何为空 “我的视图控制器”在加载视图后立即实例化UIView“theGrid” 后来,我在另一个类(MyOtherClass)中进行了切换,该类调用UIViewContoller-(void)updateTheGrid:(id)sender方法,该方法根据NSLog进行调用,但当我输出UIView以查看它是否存在时,它返回null 我做错了什么?我的印象是ARC能跟上一切。当我ViewController*vc=[[ViewController alloc]
-(void)updateTheGrid:(id)sender
方法,该方法根据NSLog进行调用,但当我输出UIView以查看它是否存在时,它返回null
我做错了什么?我的印象是ARC能跟上一切。当我ViewController*vc=[[ViewController alloc]init]时,我觉得我的麻烦来自mm“MyOtherClass”因为我觉得这只是在创建一个新实例。但是如果是这样,我该如何引用旧实例并调用该方法呢
NSLOG输出
[28853:c07] Intial Grid: <GridView: 0x8e423b0; frame = (0 0; 768 1024); layer = <CALayer: 0x8e43780>>
[28853:c07] Update The Grid (null)
ViewController.h
#import <UIKit/UIKit.h>
@interface GridView : UIView
- (void) gridUpdated;
@end
#import <UIKit/UIKit.h>
#import "GridView.h"
@interface ViewController : UIViewController {
GridView *theGrid;
}
@property (strong, retain) GridView * theGrid;
- (void) updateTheGrid : (id) sender;
@end
@property(nonatomic,weak) ViewController *vc;
MyOtherClass.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
#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
- (void) mySwitch : (id) sender {
ViewController * vc = [[ViewController alloc] init];
[vc updateTheGrid:sender];
}
- (void) mySwitch : (id) sender {
[self.vc updateTheGrid:sender];
}
不要在您的MyOtherClass.m
中再次分配ViewController
对象,因为它将创建ViewController
的新实例,并且包含ViewController
的以前对象将被处理,包括网格
因此,请在MyOtherClass.m
中声明ViewController
的weak
属性,并在分配MyOtherClass.m
例如:
视图控制器类
moc = [[MyOtherClass alloc] initWithFrame:self.view.frame];
moc.vc = self;
MyOtherClass.h
#import <UIKit/UIKit.h>
@interface GridView : UIView
- (void) gridUpdated;
@end
#import <UIKit/UIKit.h>
#import "GridView.h"
@interface ViewController : UIViewController {
GridView *theGrid;
}
@property (strong, retain) GridView * theGrid;
- (void) updateTheGrid : (id) sender;
@end
@property(nonatomic,weak) ViewController *vc;
MyOtherClass.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
#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
- (void) mySwitch : (id) sender {
ViewController * vc = [[ViewController alloc] init];
[vc updateTheGrid:sender];
}
- (void) mySwitch : (id) sender {
[self.vc updateTheGrid:sender];
}
注意:注意转发声明:)你的updateThePaper方法在哪里?这是什么意思“@property(strong,retain)GridView*theGrid;”?对不起,我更新了代码来编辑打字错误。现在应该是updateTheGrid。至于'@property(strong,retain)GridView*theGrid;'我尝试了不同的方法来维护内存中的网格,或者至少是一种引用它的方法。我需要那条线吗@Ramshad问题不是在这里发生的。。如果你不介意的话,你可以将示例应用发送到ramshadram90@gmail.com ? 我可以帮你…发送到你的电子邮件地址。它在一个zip文件@RamshadI中,我已经发布了答案。。。