2012-09-06 62 views
4

我遇到了一個問題 - 在試圖設置UIWebView.delegate = self時,我得到了EXC_BAD_ACCESS;UIWebView委託中的EXC_BAD_ACCESS

我的代碼:

vkLogin.h -

#import UIKit/UIKit.h 

@interface vkLogin : UIViewController <UIWebViewDelegate> 
{ 
    UIWebView *authBrowser; 
    UIActivityIndicatorView *activityIndicator; 
} 

@property (nonatomic, retain) UIWebView *authBrowser; 
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator; 

@end 

vkLogin.m -

#import "vkLogin.h" 
#import "bteamViewController.h" 

@implementation vkLogin 

@synthesize authBrowser; 

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityIndicator.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); 
    activityIndicator.autoresizesSubviews = YES; 
    activityIndicator.hidesWhenStopped = YES; 

    [self.view addSubview: activityIndicator]; 
    [activityIndicator startAnimating]; 

    authBrowser = [[UIWebView alloc] initWithFrame:self.view.bounds]; 

    authBrowser.delegate = self; 
    authBrowser.scalesPageToFit = YES; 

    [self.view addSubview:authBrowser]; 

    NSString *authLink = @"http://api.vk.com/oauth/authorize?client_id=-&scope=audio&redirect_uri=http://api.vk.com/blank.html&display=touch&response_type=token"; 
    NSURL *url = [NSURL URLWithString:authLink]; 

    [authBrowser loadRequest:[NSURLRequest requestWithURL:url]]; 

} 

- (void) webViewDidFinishLoad:(UIWebView *)authBrowser 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Lol" message:@"OLOLO" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil]; 

    [alert show]; 

} 
@end 

所以,如果我commeting委託串 - 一切工作正常,但我沒有收不到我的webViewDidFinishLoad事件。

我在做什麼錯了?

+1

除了問題之外,恭喜你成爲100,000個客觀標記問題! – TheAmateurProgrammer

+0

以這種方式指定'authlink'並重試:'NSString * authLink = @「http://api.vk.com/oauth/authorize?client_id=-&scope=audio&redirect_uri=http://api.vk.com/blank .html&display = touch&response_type = token「;' – Adam

+0

我建議編輯你的方案並打開殭屍對象。它很可能提供有關錯誤訪問內容的更好信息。 –

回答

5

錯誤不在您發佈的代碼中。你的殭屍消息是說你對vkLogin的引用不好。所以你需要看看什麼課程創建,並持有對你的課程的參考。

該類應該做的像vkLogin *foo = [[vkLogin alloc] init];

更新的東西:

根據您的意見,它看起來像你的vkLogin創建一個局部變量。看到代碼創建並使用vkLogin以及它的調用方式是非常有用的。除了這個,這裏有一些猜測。

您被稱爲創建並不止一次地向子視圖添加vkLogin的方法。 (每次都會創建一個新實例)。 您在刪除vkLogin後會發生某種回撥。

我的猜測是vkLogin應該是你的類中的property,而不是本地方法變量。

在您的.h

你想補充 @proprerty (strong, nonatomic) vkLogin *vk;

,並在您.m文件,你可以把它稱爲self.vk所以你創建它,並把它添加像一個子視圖:

self.vk = [[vkLogin alloc] init]; 
[self.view addSubview:self.vk]; 

在附註中,約定表示我們應該使用大寫字母開始類名,所以您可以命名類VkLogin,這可以使它很容易與名爲vkLogin的變量區分開來(但是在解決問題後會擔心)

+0

是的,我正在使用ARC。是的,就像你已經發布:vkLogin * vk = [[vkLogin alloc] init]; [self。查看addSubView:vk]; – Danny

+0

哦,非常感謝你!這真的有幫助。我收到了你的筆記,現在我將用大寫字母開始課程名稱。 :) – Danny