ios - Programatically create navigation controller with back button -
i haven't been able figure out in swift in appdelegate. i've been working on little problem weeks (in background, not continuously) lots of googling. must missing something.
when remote notification comes in ios app i'd modally present view appropriate message, uinavigationcontroller has button. idea when user finished dealing notification, can press "back" , go were. can't "back" button show in navigation controller programatically.
not of existing views embedded in navigation controllers. so, rather finding existing uinavigationcontroller in stack, i've been creating new uinavigationcontroller , trying set , present this:
let mainstoryboard: uistoryboard = uistoryboard(name: "main", bundle: nil) var mvc = mainstoryboard.instantiateviewcontrollerwithidentifier("myviewidentifier") as! myspecialviewcontroller /* here setup necessary variables in mvc view controller, using data remote message. don't need see */ /* seems work, looks navigation controller */ let newnavcontroller = uinavigationcontroller(rootviewcontroller: mvc) /* part doesn't work, don't "back" button */ let btn = uibarbuttonitem(title: "back", style: uibarbuttonitemstyle.done, target: mvc, action: "backpressed") newnavcontroller.navigationitem.leftbarbuttonitem = btn /* block seems work. idea work through hierarchy of modal presentations until screen showing right now. i'm not sure work in situations, seems work in app's hierarchy. */ var currentviewcontroller = self.window!.rootviewcontroller while currentviewcontroller?.presentedviewcontroller != nil { currentviewcontroller = currentviewcontroller?.presentedviewcontroller } /* suppose shows new view controller modally, except without "back" button it's hard sure navigation controller there. */ currentviewcontroller?.presentviewcontroller(newnavcontroller, animated: true, completion: nil)
i have method called "backpressed" in relevant view controller.
any clues why navigation bar button isn't showing up?
edit: knew backbarbuttonitem shows when aren't @ top of stack. but, leftbarbuttonitem should show if we're @ top of stack?
here simple example can set navigation bar programmatically first view:
if let resultcontroller = storyboard!.instantiateviewcontrollerwithidentifier("secondview") as? secondview { let navcontroller = uinavigationcontroller(rootviewcontroller: resultcontroller) // creating navigation controller resultcontroller @ root of navigation stack. self.presentviewcontroller(navcontroller, animated:true, completion: nil) }
if wan add button navigation use code secondview.swift
class:
override func viewdidload() { super.viewdidload() let backbutton = uibarbuttonitem(title: "back", style: uibarbuttonitemstyle.plain, target: self, action: "goback") navigationitem.leftbarbuttonitem = backbutton } func goback(){ dismissviewcontrolleranimated(true, completion: nil) }
if want of firstview
here code:
@ibaction func btnpressed(sender: anyobject) { if let resultcontroller = storyboard!.instantiateviewcontrollerwithidentifier("secondview") as? secondview { resultcontroller.navigationitem.leftbarbuttonitem = uibarbuttonitem(title: "back", style: uibarbuttonitemstyle.plain, target: self, action: "goback") let navcontroller = uinavigationcontroller(rootviewcontroller: resultcontroller) // creating navigation controller vc1 @ root of navigation stack. self.presentviewcontroller(navcontroller, animated:true, completion: nil) } } func goback(){ dismissviewcontrolleranimated(true, completion: nil) }
hope you.
Comments
Post a Comment