2012-04-28 61 views
0

將局部變量改爲實例變量後,我的代碼出現問題。具體而言,我一直有更改一行代碼後,我在我的HypnosisterAppDelegate.m文件代碼的麻煩:將局部變量改爲實例變量後的重構

HypnosisView *view = [[HypnosisView alloc]initWithFrame:screenRect]; 

view = [[HypnosisView alloc]initWithFrame:screenRect]; 

我在HypnosisterAppDelegate註釋掉的錯誤信息.m你能告訴我什麼是錯的嗎?提前致謝。

HypnosisterAppDelegate.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 

HypnosisView.h

#import <Foundation/Foundation.h> 

@interface HypnosisView : UIView { 

} 

@property(nonatomic,strong)UIColor *circleColor; 
@end 
+1

你在哪裏創建'scrollView'? – 2012-04-28 19:57:57

+0

認爲這是問題:必須意外刪除了一行:UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect];當我將它添加回來時,它很有用。謝謝。 – pdenlinger 2012-04-28 20:14:25

+0

很高興你懂了! – 2012-04-28 20:15:08

回答

0

您沒有一個變量,名爲scrollView任何地方。我猜(尤其是來自評論// Create the UIScrollView to have the size of a window, matching its size,也已經通過這本書我自己走了)那你的意思是創建一個UIScrollView

// not: view = [[HypnosisView alloc]initWithFrame:screenRect]; 
// but: 
UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:screenRect]; 

,然後添加你的HypnosisView後來作爲一個子視圖。