問題是我不知道如何在推送或呈現爲模態視圖後保存接收到的控制器數據。我正在使用故事板,ARC,iOS5。當項目使用xib時,一切正常。我在目標ViewController中有UIWebView,我需要它爲twitter OAuth身份驗證打開傳輸的URL。這裏是TweetViewController的代碼,我從那裏調用WebViewController的函數。然後我嘗試在UIWebController所在的位置展示UIViewController。但是在我傳輸url之後,它忘記了這個數據。所以在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的它發送URL數據:所以在這裏
#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)
高興你發現了它。但爲了安全起見,您可能還想檢查initWithURL方法周圍的內存泄漏。我無法確定,但當您運行故事板instantiateViewControllerWithIdentifier時,可能會發生泄漏。祝你好運! – Rob 2012-02-02 16:39:19