如何从自定义视图控制器类加载初始视图控制器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从自定义视图控制器类加载初始视图控制器相关的知识,希望对你有一定的参考价值。
在我的storyboard
我有一个FirstViewController
,也是一个Initial View Controller
。我在我的UIViewController
中有另一个StoryBoard
,名为BaseViewController
。我在这上面加了一个NavigationBar
。我想从FirstViewController
派生出BaseViewController
,所以我写了下面的代码。
class FirstViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
但是当我运行App时,它没有显示BaseViewController
的内容。相反,它只加载FirstViewController
的内容。我想要从UIViewControllere
派生的每一个BaseViewController
应该有它的NavigationBar
。我需要改变才能得到这个?
答案
如果您的BaseViewController的目的只是为ViewControllers提供NavigationBar。您可以将您的FirstViewController(NavController的rootcontroller)嵌入NavigationController(设置为初始视图控制器)。如果你想为不同的控制器使用不同的导航栏,你必须像我一样以编程方式设置它。
看看这里 -
extension UIViewController {
func setNavBar(title : String, modType : ModuleType = .home, barHexColor : String? = nil, isBackBtn : Bool, isMenuBtn : Bool, isJummperBtn : Bool, isScreenshotBtn : Bool) {
k_ModuleType = modType
var barColor = "E14145"
var arrLeftItems = [UIBarButtonItem]()
var arrRightItems = [UIBarButtonItem]()
if let aBarColor = barHexColor {
barColor = aBarColor
}
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(hexString: barColor)
self.navigationController?.navigationBar.tintColor = UIColor.white
if isBackBtn {
let btnBack = UIButton(type: .custom)
btnBack.setImage(UIImage(named: "Back2"), for: .normal)
btnBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnBack.addTarget(self, action: #selector(self.barBackAction(_:)), for: .touchUpInside)
let itemBack = UIBarButtonItem(customView: btnBack)
arrLeftItems.append(itemBack)
}
if isMenuBtn {
let btnMenu = UIButton(type: .custom)
btnMenu.setImage(UIImage(named: "menu2"), for: .normal)
btnMenu.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnMenu.addTarget(self, action: #selector(self.barMenuAction(_:)), for: .touchUpInside)
let itemMenu = UIBarButtonItem(customView: btnMenu)
arrLeftItems.append(itemMenu)
}
if isScreenshotBtn {
let btnScreenshot = UIButton(type: .custom)
btnScreenshot.setImage(UIImage(named: "screenshot2"), for: .normal)
btnScreenshot.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnScreenshot.addTarget(self, action: #selector(self.barScreenshotAction(_:)), for: .touchUpInside)
let itemScreenshot = UIBarButtonItem(customView: btnScreenshot)
arrRightItems.append(itemScreenshot)
}
if isJummperBtn {
let btnJumpper = UIButton(type: .custom)
btnJumpper.setImage(UIImage(named: "jumpper2"), for: .normal)
btnJumpper.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnJumpper.addTarget(self, action: #selector(self.barJumpperAction(_:)), for: .touchUpInside)
let itemJumpper = UIBarButtonItem(customView: btnJumpper)
arrRightItems.append(itemJumpper)
}
self.navigationItem.setLeftBarButtonItems(arrLeftItems, animated: true)
self.navigationItem.setRightBarButtonItems(arrRightItems, animated: true)
let lblNavTitle = UILabel(frame: CGRect(x: 0, y: 0,
width: DeviceType.isIPad() ? 200 : 110,
height: 40))
lblNavTitle.textAlignment = .center
lblNavTitle.numberOfLines = 0
lblNavTitle.textColor = UIColor.white
lblNavTitle.text = title
lblNavTitle.font = UIFont.systemFont(ofSize: DeviceType.isIPad() ? 21 : 16,
weight: .medium)
lblNavTitle.backgroundColor = UIColor.clear
self.navigationItem.titleView = lblNavTitle
self.navigationItem.titleView?.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapTitle(_:)))
tapGesture.numberOfTapsRequired = 1
self.navigationItem.titleView?.addGestureRecognizer(tapGesture)
}
然后在FirstViewController的Viewdidload()方法中
override func viewdidload()
{
super.viewdidload()
self.setNavBar(title : aTitle,
barHexColor : nil,
isBackBtn : false,
isMenuBtn : true,
isJummperBtn : true,
isScreenshotBtn : true)
}
另一答案
@interface CommonViewController ()<UITextFieldDelegate>//,UITableViewDelegate,UITableViewDataSource>
{
APIFactory *fireAPI;
Supportive *support;
MBProgressHUD *HUD;
BOOL blinkStatus;
}
@end
@implementation CommonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
[super viewDidLoad];
fireAPI = [[APIFactory alloc] init];
support = [[Supportive alloc] init];
self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"IntelliDealer.sqlite"];
self.dataArray = [[NSMutableArray alloc]init];
self.strVC=@"OrderViewController";
[self setNavigationBar];
if (self.epcCartOrder.count==0) {
lblBadge.hidden = YES;
badgeImgView.hidden = YES;
}
else{
lblBadge.hidden = NO;
badgeImgView.hidden = NO;
}
}
-(void)setNavigationBar{
NSString *query1;
query1 =[NSString stringWithFormat:@"SELECT * FROM Dealer_Spare_Order WHERE spareOrderListId=%d ORDER BY spareAddTime DESC",[[[UICKeyChainStore keyChainStore] stringForKey:@"DealerEnableOrderList"] intValue]];
self.epcCartOrder = [[NSMutableArray alloc] initWithArray:[self.dbManager loadDataFromDB:query1]];
[Supportive ShowLog:[NSString stringWithFormat:@"no of rows => %lu",(unsigned long)self.epcCartOrder.count]];
self.navigationController.navigationBarHidden = YES;
CustomTitleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, iPhoneX ? 100:70)];
CustomTitleView.backgroundColor = [UIColor colorWithRed:231/255.f green:66/255.f blue:57/255.f alpha:1.0f];
backButton =[UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(didclickOnBackButton) forControlEvents:UIControlEventTouchUpInside];
backButtonImage = [UIImageView new];
menuButton =[UIButton buttonWithType:UIButtonTypeCustom];
menuButton.tag = 100;
[menuButton addTarget:self action:@selector(didclickOnMenuButton) forControlEvents:UIControlEventTouchUpInside];
lblDealerName = [[UILabel alloc] init];
lblDealerName.numberOfLines=3;
lblDealerName.text= [[UICKeyChainStore keyChainStore] stringForKey:@"DealerName"];
lblDealerName.textAlignment=NSTextAlignmentCenter;
lblDealerName.textColor = [UIColor whiteColor];
lblDealerName.backgroundColor=[UIColor clearColor];
lblDealerName.userInteractionEnabled=NO;
btnDealerName =[UIButton buttonWithType:UIButtonTypeCustom];
[btnDealerName addTarget:self action:@selector(didclickOnDealerName) forControlEvents:UIControlEventTouchUpInside];
[btnDealerName addSubview:lblDealerName];
addToCartButton =[UIButton buttonWithType:UIButtonTypeCustom];
[addToCartButton addTarget:self action:@selector(didclickOnAddToCartButtonButton) forControlEvents:UIControlEventTouchUpInside];
addToCartButtonImage = [UIImageView new];
[self.view addSubview:CustomTitleView];
[CustomTitleView addSubview:backButtonImage];
[CustomTitleView addSubview:backButton];
[CustomTitleView addSubview:menuButton];
[CustomTitleView addSubview:btnDealerName];
//[CustomTitleView addSubview:cartButton];
[CustomTitleView addSubview:addToCartButtonImage];
[CustomTitleView addSubview:addToCartButton];
if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPad) {
[backButton setFrame:CGRectMake(0, 8, 45, 60)];
// [backButton setImage:[UIImage imageN以上是关于如何从自定义视图控制器类加载初始视图控制器的主要内容,如果未能解决你的问题,请参考以下文章