如何在不丢失目标控制器中的数据的情况下打开模态视图?

时间:2012-02-02 14:27:05

标签: objective-c xcode ios5 uiwebview storyboard

问题是我不知道如何在推送或呈现为模态视图后保存接收到的控制器数据。我正在使用故事板,ARC,iOS5。当项目使用xib时一切正常。我在目标ViewController中有UIWebView,我需要它来打开Twitter OAuth身份验证的传输URL。这是TweetViewController的代码,我从这里调用WebViewController的函数。然后我尝试呈现UIWebController所在的UIViewController。但在我传输网址后,它会忘记这些数据。因此,在ViewDidLoad函数上,NSURL为空。

// 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

发送网址数据的TweetViewController.m的一部分:

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

和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 viewDidLoad调用的日志消息。 currentURL =(null)

1 个答案:

答案 0 :(得分:1)

我注意到的是你正在调用故事板来在initWithURL方法中创建视图控制器。创建对象的正常方式是执行alloc,然后执行initWithURL。 alloc方法在内存中创建对象,initWithURL配置对象。如果您正在调用alloc然后在initWithURL中调用instantiateViewControllerWithIdentifier,则可能会创建内存泄漏,因为instantiateViewControllerWithIdentifier在调用时也会分配内存。然后将“self”设置为另一个地址。

当您运行instantiateViewControllerWithIdentifier时,这可能会导致混淆。这很难说。您应该在父视图控制器中运行instantiateViewControllerWithIdentifier,然后推送视图控制器,然后设置您的URL,因为UIWebViews仅在显示时实例化,而不是在创建视图控制器时实例化。

HTH