ios - Open UIViewController upon clicking on UITabbarItem -
i have created uiviewcontroller uitabbar in it.
i did not use uitabbarcontroller because wanted uitabbar on top of screen.
upon clicking tab1, want present controller1 , on clicking tab2 want present controller 2. don't want tabbar hide. want display controller beneath tabbar.
@interface mtleaderfactoviewcontroller () <uitabbardelegate> @property (weak, nonatomic) iboutlet uitabbar *tabbar; @end @implementation mtleaderfactoviewcontroller - (void)viewdidload { [super viewdidload]; } - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; } - (void)viewwilldisappear:(bool)animated { [super viewwilldisappear:animated]; } - (void)tabbar:(uitabbar *)tabbar didselectitem:(uitabbaritem *)item { if (item.tag == 0) { nslog(@"item tag 0"); } else { nslog(@"item tag 1"); } } @end
my questions:
1) didselectitem method not triggered after using uitabbardelegate
2) elegant way of displaying controller when clicked on button? don't want use segue controllers in different storyboards. now, plan do
controller1 *fp = [controller1 controllerstoryboard:storyboard_course]; [self addchildviewcontroller:fp]; [self.view addsubview:fp.view]; [fp didmovetoparentviewcontroller:self];
edit 1:
controller1 *fp = [controller1 controllerstoryboard:storyboard_course]; [self addchildviewcontroller:fp]; [self.view addsubview:fp.view]; [fp didmovetoparentviewcontroller:self];
tried hides tab bar. want utilize space beneath tab bar display controller
what need have basecontroller class contain tabbar(programatically created) can achive desired output heres sample basecontroller created,
import uikit class baseviewcontroller: uiviewcontroller,uitabbardelegate{ override func viewdidload() { super.viewdidload() let mytabbar = uitabbar() mytabbar.frame = cgrect(x: 0, y: 60, width:self.view.frame.size.width, height: 50) let 1 = uitabbaritem() one.title = "one" one.tag = 1 let 2 = uitabbaritem() two.title = "two" two.tag = 2 mytabbar.setitems([one,two], animated: false) self.view.addsubview(mytabbar) mytabbar.delegate = self } func tabbar(_ tabbar: uitabbar, didselect item: uitabbaritem) { switch item.tag { case 1: let controller = storyboard?.instantiateviewcontroller(withidentifier: "secondviewcontroller") addchildviewcontroller(controller!) view.addsubview((controller?.view)!) controller?.didmove(toparentviewcontroller: self) break case 2: let controller = storyboard?.instantiateviewcontroller(withidentifier: "viewcontroller") addchildviewcontroller(controller!) view.addsubview((controller?.view)!) controller?.didmove(toparentviewcontroller: self) break default: break } } }
view controller class :
import uikit class viewcontroller: baseviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } }
secondview controller :
import uikit class secondviewcontroller: baseviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view. } }
Comments
Post a Comment