我需要子类化SVModalWebViewController.m(GitHub),它本身就是UINavigationController
的子类,并将此代码合并到一个完全在Swift中创建的新应用程序中。我遇到了几个问题,所以我决定先把这个非常简单的类转换成Swift来调试。
与Objective-C(read more about Swift initialization here)相比,Swift中的初始化略有不同,这就是为什么我没有在Swift代码中返回self
。
这是原始的Obj-C代码(减去* .h,它实例化barsTintColor
):
#import "SVModalWebViewController.h"
#import "SVWebViewController.h"
@interface SVModalWebViewController ()
@property (nonatomic, strong) SVWebViewController *webViewController;
@end
@implementation SVModalWebViewController
#pragma mark - Initialization
- (id)initWithAddress:(NSString*)urlString {
return [self initWithURL:[NSURL URLWithString:urlString]];
}
- (id)initWithURL:(NSURL *)URL {
self.webViewController = [[SVWebViewController alloc] initWithURL:URL];
if (self = [super initWithRootViewController:self.webViewController]) {
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self.webViewController
action:@selector(doneButtonClicked:)];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
self.webViewController.navigationItem.leftBarButtonItem = doneButton;
else
self.webViewController.navigationItem.rightBarButtonItem = doneButton;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
self.webViewController.title = self.title;
self.navigationBar.tintColor = self.barsTintColor;
}
@end
这是该类的整个Swift版本(我简化为一个init方法,因为我不需要使用URL专门初始化):
import UIKit
class SVModalWebViewController: UINavigationController {
var webViewController: SVWebViewController!
var barsTintColor: UIColor?
// This was added because of a compiler error indicating it needed to be added
init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
init(address urlString: String!) {
let url: NSURL = NSURL.URLWithString(urlString)
self.webViewController = SVWebViewController(URL: url)
super.init(rootViewController: self.webViewController)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self.webViewController, action: Selector("doneButtonClicked:"))
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
self.navigationItem.leftBarButtonItem = doneButton
} else {
self.navigationItem.rightBarButtonItem = doneButton
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// self.title may not be set, and is considered an optional in Swift, so we have to check first
if self.title {self.webViewController.title = self.title}
self.navigationBar.tintColor = self.barsTintColor
}
}
我遇到的问题与在Done
中设置navigationItem
按钮有关。目前,在调用此代码时,它们根本没有出现:
let webViewController = SVModalWebViewController(address: "http://www.google.com");
webViewController.barsTintColor = UIColor.blueColor()
self.presentModalViewController(webViewController, animated: true)
模态视图显示正常,我可以正确设置条形颜色属性,但Done
按钮不会显示。我似乎无权访问navigationItem
的{{1}}。请注意,由于没有它的编译器错误,我必须使用nibName方法添加init。这在Obj-C代码中不是必需的,我承认我不确定为什么在Swift中需要它 - 这可能是问题的一部分。
有关为何我无法设置UINavigationController
的{{1}}属性的任何想法?谢谢!
答案 0 :(得分:0)
UINavigationController没有navigationItem
。嗯,确实如此,但它没用。它是导航栏中显示的子控件的navigationItem
(在本例中为rootViewController
,您也称为self.webViewController
)。