Objective c 局部变量变为实例变量后的重构
在将局部变量更改为实例变量后,我的代码遇到了一些问题。具体地说,在更改以下代码的一行后,我在催眠器appdelegate.m文件中的代码遇到了问题:Objective c 局部变量变为实例变量后的重构,objective-c,Objective C,在将局部变量更改为实例变量后,我的代码遇到了一些问题。具体地说,在更改以下代码的一行后,我在催眠器appdelegate.m文件中的代码遇到了问题: HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect]; 到 我已经注释掉了催眠器AppDelegate.m中的错误消息。你能告诉我出了什么问题吗?提前谢谢 催眠器 #import "HypnosisterAppDelegate.h" #import "Hypnosi
HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect];
到
我已经注释掉了催眠器AppDelegate.m中的错误消息。你能告诉我出了什么问题吗?提前谢谢
催眠器
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
@implementation HypnosisterAppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
CGRect screenRect = [[self window]bounds];
// Create the UIScrollView to have the size of a window, matching its size
view = [[HypnosisView alloc]initWithFrame:screenRect];
[scrollView setMinimumZoomScale:1.0]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
[scrollView setMaximumZoomScale:5.0]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
//You will get a warning here, ignore it for now
[scrollView setDelegate:self]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
[[self window] addSubview:scrollView]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
//Create the HypnosisView with a frame that is twice the size of the screen
CGRect bigRect = screenRect;
HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect];
// Add the HypnosisView as a subview of the scrollView instead of the window
[scrollView addSubview:view]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
// Tell the scrollView how big its virtual world is
[scrollView
setContentSize:bigRect.size]; // Unknown receiver "scrollView"; should it be "UIScrollView"?
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(@"HypnosisView became the first responder");
} else {
NSLog(@"Could not become the first responder");
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return view;
}
@end
催眠视图
#import <Foundation/Foundation.h>
@interface HypnosisView : UIView {
}
@property(nonatomic,strong)UIColor *circleColor;
@end
#导入
@界面催眠视图:UIView{
}
@性质(非原子,强)UIColor*圆环色;
@结束
任何地方都没有名为scrollView
的变量。我猜(特别是从注释//创建与窗口大小相匹配的UIScrollView,我自己也看过这本书)您是想创建一个UIScrollView
:
// not: view = [[HypnosisView alloc]initWithFrame:screenRect];
// but:
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
然后将您的催眠视图
添加为子视图。您在哪里创建滚动视图
?认为这就是问题所在:一定是意外删除了一行:UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:screenRect];当我把它加回去的时候,它起作用了。谢谢。谢谢。你想把你的评论放在回答区,这样我可以给你积极的反馈吗?如果你愿意的话。我的评论是为了修辞,但我很高兴它是有帮助的。(如果您愿意,您可以在自己解决问题时回答自己的问题,尽管您需要等待几个小时才能发帖。)
// not: view = [[HypnosisView alloc]initWithFrame:screenRect];
// but:
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect];