2012-02-02 1 views
0

문제는 내가받은 컨트롤러 데이터를 밀어 넣거나 모달보기로 제시 한 후에 어떻게 저장할 수 있는지 잘 모르는 것입니다. 스토리 보드, ARC, iOS5를 사용하고 있습니다. 프로젝트가 xib를 사용할 때 모든 것이 잘 작동했습니다. 대상 ViewController UIWebView 있고 트위터 OAuth 인증을 위해 전송 된 URL을 열 필요가. 다음은 WebViewController의 함수를 호출하는 TweetViewController 코드입니다. 그런 다음 UIWebController가있는 UIViewController를 표시하려고합니다. 그러나 URL을 전송 한 후에는이 데이터를 잊어 버립니다. 따라서 ViewDidLoad 함수에서 NSURL은 null입니다. URL 데이터 전송 TweetViewController.m의대상 컨트롤러에서 데이터 손실없이 모달 뷰를 열려면 어떻게해야합니까?

// TweetViewController.h 
#import <UIKit/UIKit.h> 
#import "RSTwitterEngine.h" 
#import "WebViewController.h" 

@interface TweetViewController : UIViewController <RSTwitterEngineDelegate, WebViewControllerDelegate> 

@property (strong, nonatomic) RSTwitterEngine *twitterEngine; 
@property (strong, nonatomic) WebViewController *leWebView; 

@property (unsafe_unretained, nonatomic) IBOutlet UITextView *textView; 
@property (unsafe_unretained, nonatomic) IBOutlet UIBarButtonItem *sendButton; 
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *statusLabel; 

- (IBAction)sendTweet:(id)sender; 
- (void)swipedRight:(UIGestureRecognizer *)recognizer; 

@end 

부 :에서 WebViewController.m

#import "WebViewController.h" 
#import "AppDelegate.h" 

@implementation WebViewController 

@synthesize delegate = _delegate; 
@synthesize currentURL = _currentURL; 
@synthesize leWebView = _leWebView; 
@synthesize leActivityIndicator = _leActivityIndicator; 

#pragma mark - Initialization 
// ======= Here is that function which recieves url ======== 
- (id)initWithURL:(NSURL *)url 
{ 
    self = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"]; 
    NSLog(@"WebViewController initWithURL called. Recieved url = %@", url); 

    if (self) { 
     self.currentURL = url; 
     self.delegate = nil; 
    } 

    return self; 

} 

#pragma mark - View Lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"WebViewController viewDidLoad called. currentURL = %@", self.currentURL); 

    self.leWebView.scalesPageToFit = YES; 
    [self.leWebView loadRequest:[NSURLRequest requestWithURL:self.currentURL]];  
} 

- (void)viewDidUnload 
{ 
    [self setLeWebView:nil]; 
    [self setLeActivityIndicator:nil]; 

    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - UIWebView Delegate Methods 

- (BOOL)LeWebView:(UIWebView *)LeWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if ([request.URL.scheme isEqualToString:@"rstwitterengine"] && (self.delegate)) { 
     if (self.leActivityIndicator) [self.leActivityIndicator stopAnimating]; 
     [self.delegate handleURL:request.URL]; 
     return NO; 
    } else { 
     return YES; 
    } 
} 

- (void)webViewDidStartLoad:(UIWebView *)LeWebView 
{ 
    if (self.leActivityIndicator) [self.leActivityIndicator startAnimating]; 
} 

- (void)webViewDidFinishLoad:(UIWebView *)LeWebView 
{ 
    if (self.leActivityIndicator) [self.leActivityIndicator stopAnimating]; 
} 

- (void)LeWebView:(UIWebView *)LeWebView didFailLoadWithError:(NSError *)error 
{ 
    if (self.leActivityIndicator) [self.leActivityIndicator stopAnimating]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                message:[error localizedDescription] 
                delegate:nil 
              cancelButtonTitle:@"Dismiss" 
              otherButtonTitles:nil]; 
    [alert show]; 
} 

#pragma mark - Custom Methods 


- (IBAction)ActionNoMore:(id)sender { 
    if (self.delegate) [self.delegate dismissWebView]; 

} 
@end 

// WebViewController.h 

#import <UIKit/UIKit.h> 

@protocol WebViewControllerDelegate; 

@interface WebViewController : UIViewController <UIWebViewDelegate> 

@property (retain, nonatomic) NSURL *currentURL; 

@property (assign, nonatomic) id <WebViewControllerDelegate> delegate; 
@property (retain, nonatomic) IBOutlet UIWebView *leWebView; 
@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *leActivityIndicator; 

- (id)initWithURL:(NSURL *)url; 
- (IBAction)ActionNoMore:(id)sender; 
@end 

@protocol WebViewControllerDelegate <NSObject> 

- (void)dismissWebView; 
- (void)handleURL:(NSURL *)url; 

@end 

과 : 그래서 여기

#pragma mark - RSTwitterEngine Delegate Methods 

- (void)twitterEngine:(RSTwitterEngine *)engine needsToOpenURL:(NSURL *)url 
{ 
    self.leWebView = [_leWebView initWithURL:url]; 
    self.leWebView.delegate = self; 
    [self performSegueWithIdentifier:@"LeWebSegue" sender:self];  
    //[self presentModalViewController:self.leWebView animated:YES]; 
} 

는 WebViewController 핵심 부품이다 모두,로드하려고 할 때 그보기, 나는 WebViewController viewDidLoad라는 로그 메시지를받습니다. currentURL = (null)

답변

1

내가 알아챈 점은 스토리 보드를 호출하여 initWithURL 메소드에서보기 컨트롤러를 생성한다는 것입니다. 객체가 생성되는 일반적인 방법은 alloc과 initWithURL입니다. alloc 메소드는 객체를 메모리에 만들고 initWithURL은 객체를 구성합니다. alloc을 호출 한 다음 initWithURL에서 instantiateViewControllerWithIdentifier를 호출하면 InstantiateViewControllerWithIdentifier가 호출 될 때 메모리를 할당하므로 메모리 누수가 발생할 수 있습니다. 그런 다음 "자기"를 다른 주소로 설정합니다.

instantiateViewControllerWithIdentifier를 실행할 때 혼동을 일으킬 수 있습니다. 말하기 어렵습니다. 당신은 아마 부모 뷰 컨트롤러에 instantiateViewControllerWithIdentifier를 실행 한 다음보기 컨트롤러를 밀어 다음 뷰 컨트롤러가 생성 될 때 UIWebViews의는 그들 만이 표시 될 때 인스턴스화되지 이후 URL을 설정해야합니다.

HTH

+0

당신을 발견해 주셔서 감사합니다. 하지만 안전을 기하기 위해 initWithURL 메소드를 통해 메모리 누수가 있는지 확인할 수도 있습니다. 나는 확신 할 수 없지만 당신은 스토리 보드 instantiateViewControllerWithIdentifier을 실행할 때 당신이 누출 될 수있다. 행운을 빕니다! – Rob

관련 문제