如何从Custom View控制器类加载Initial View Controller

时间:2019-04-25 09:12:09

标签: ios initialization storyboard derived-class

在我的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.
    } 

但是当我运行该应用程序时,它没有显示BaseViewController的内容。相反,它仅加载FirstViewController的内容。我希望从UIViewControllere派生的每个BaseViewController都应有其NavigationBar。我需要 改变以得到这个?

2 个答案:

答案 0 :(得分:0)

如果BaseViewController的目的仅仅是向ViewControllers提供NavigationBar。您可以将FirstViewController(NavController的rootcontroller)嵌入到NavigationController(设置为Initial View Controller)中。 如果要为不同的控制器使用不同的导航栏,则必须像我一样以编程方式进行设置。

在这里看看-

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)
}

答案 1 :(得分:0)

@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 imageNamed:@"back_arrow"] forState:UIControlStateNormal];

        [backButtonImage setFrame:CGRectMake(0, 18, 40, 40)];
        [backButtonImage setImage:[UIImage imageNamed:@"back_arrow"]];

        [menuButton setFrame:CGRectMake(50, 18, 40, 40)];
        [menuButton setImage:[UIImage imageNamed:@"ipadnavigatio"] forState:UIControlStateNormal];

        [btnDealerName setFrame:CGRectMake(100, 24, 590, 30)];

        [lblDealerName setFrame:CGRectMake(0, 0, 590, 30)];
        [lblDealerName setTextAlignment:NSTextAlignmentCenter];

        lblDealerName.font = [UIFont fontWithName:@"Arial" size:22];

       [addToCartButton setFrame:CGRectMake(690, 10, 70, 60)];

        [addToCartButtonImage setFrame:CGRectMake(700, 23, 40, 38)];
        [addToCartButtonImage setImage:[UIImage imageNamed:@"ipadaddtocart"]];

        badgeImgView = [[UIImageView alloc]init];


        //badgeImgView.hidden =YES;

        badgeImgView.image = [UIImage imageNamed:@"order_bg"];
        lblBadge = [[UILabel alloc] init];


        if (self.epcCartOrder.count<10) {

            badgeImgView.frame =CGRectMake(36,9, 23, 23);
            lblBadge.frame = CGRectMake(7, 0, 23, 23);

        }
        else{
            badgeImgView.frame =CGRectMake(38,9, 25, 25);
            lblBadge.frame = CGRectMake(4, 0, 25, 25);
        }



        /*important--------- */
        lblBadge.textColor = [UIColor whiteColor];
        lblBadge.font = [UIFont fontWithName:@"Arial" size:15];

        lblBadge.backgroundColor=[UIColor clearColor];
        lblBadge.textColor=[UIColor whiteColor];
        lblBadge.userInteractionEnabled=NO;
        //lblBadge.hidden = YES;
        lblBadge.text=[NSString stringWithFormat:@"%lu", (unsigned long)self.epcCartOrder.count];


        //[CustomTitleView addSubview:_textFieldSearch];
        [addToCartButtonImage addSubview: badgeImgView];
        [badgeImgView addSubview:lblBadge];
        [addToCartButton addSubview: badgeImgView];



    }
    else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiomPhone){


       double customViewWidth = self.view.frame.size.width - 40;

        [backButton setFrame:CGRectMake(5, iPhoneX ? 20:10, 40, 60)];
//        [backButton setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];

        [backButtonImage setFrame:CGRectMake(10, iPhoneX ? 55:25, 35, 35)];
        [backButtonImage setImage:[UIImage imageNamed:@"back_arrow"]];


        [menuButton setFrame:CGRectMake(50, iPhoneX ? 55:25, 35, 35)];
        [menuButton setImage:[UIImage imageNamed:@"ipadnavigatio.png"] forState:UIControlStateNormal];

        [btnDealerName setFrame:CGRectMake(90, iPhoneX ? 50:20, 180, 51)];
        [lblDealerName setFrame:CGRectMake(0, 0, 180, 51)];
         lblDealerName.font = [UIFont fontWithName:@"Arial" size:15];


        [addToCartButton setFrame:CGRectMake(customViewWidth - 8, iPhoneX ? 55:15, 50, 55)];

        [addToCartButtonImage setFrame:CGRectMake(customViewWidth , iPhoneX ? 60:30, 30, 30)];
        [addToCartButtonImage setImage:[UIImage imageNamed:@"ipadaddtocart"]];

        badgeImgView = [[UIImageView alloc]init];

        badgeImgView.image = [UIImage imageNamed:@"order_bg"];

        lblBadge = [[UILabel alloc] init];
        lblBadge.textColor = [UIColor whiteColor];
        lblBadge.font = [UIFont fontWithName:@"Arial" size:15];

        lblBadge.backgroundColor=[UIColor clearColor];
        lblBadge.textColor=[UIColor whiteColor];
        lblBadge.userInteractionEnabled=NO;
        lblBadge.text=[NSString stringWithFormat:@"%lu", (unsigned long)self.epcCartOrder.count];

        [addToCartButtonImage addSubview: badgeImgView];
        [badgeImgView addSubview:lblBadge];
        [addToCartButton addSubview: badgeImgView];

        if (self.epcCartOrder.count<10) {

            badgeImgView.frame=CGRectMake(28, iPhoneX ?  1:9, 20, 20);
            lblBadge.frame = CGRectMake(6, 0, 20, 20);

        }
        else{

            badgeImgView.frame=CGRectMake(28, 6, 22, 22);
            lblBadge.frame = CGRectMake(2, 0, 22, 22);
        }
        //lblBadge.center = badgeImgView.center;

        /*important--------- */



       // [CustomTitleView addSubview:_textFieldSearch];


    }

}