import
UIKit
class
ViewController
:
UIViewController
{
var
count = 0
var
navigationBar:
UINavigationBar
?
override
func
viewDidLoad() {
super
.viewDidLoad()
navigationBar =
UINavigationBar
(frame:
CGRectMake
(0, 20, 320, 44))
self
.view.addSubview(navigationBar!)
onAdd()
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
func
onAdd(){
count++
navigationBar?.pushNavigationItem(onMakeNavitem(), animated:
true
)
}
func
onRemove(){
if
count > 1{
count--
navigationBar?.popNavigationItemAnimated(
true
)
}
}
func
onMakeNavitem()->
UINavigationItem
{
var
navigationItem =
UINavigationItem
()
var
leftBtn =
UIBarButtonItem
(barButtonSystemItem:
UIBarButtonSystemItem
.
Add
,
target:
self
, action:
"onAdd"
)
var
rightBtn =
UIBarButtonItem
(barButtonSystemItem:
UIBarButtonSystemItem
.
Cancel
,
target:
self
, action:
"onRemove"
)
navigationItem.title =
"第\(count)个导航项"
navigationItem.setLeftBarButtonItem(leftBtn, animated:
true
)
navigationItem.setRightBarButtonItem(rightBtn, animated:
true
)
return
navigationItem
}
}
请发表评论