我创建了一个导航栏,我试图在另一个视图控制器中调用。我通过调用我分成左,中,右按钮的方法来设置它。在我的另一个控制器中,我调用navbarcontroller并尝试调用我为其设置导航工具栏的方法。没有任何事情发生,但没有崩溃。
import UIKit
class NavBarController : UIViewController{
var screenSize: CGRect!
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
setupToolBarItems()
self.navigationController?.isToolbarHidden = false
self.view!.backgroundColor = .white
}
我导航栏的方法就是这个
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
}
func showCalendarController() {
let navController = CalendarController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func showEventsController() {
let navController = EventsController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
} // top bar button setup
private func setupCenterNavButton() {
let buttonFrame = UIView(frame: CGRect(x: 0, y: 0, width: 165,
height: 20))
mainFeedButton.frame = CGRect(x: 0,y: 0, width: 80,height: 20) as
CGRect
mainFeedButton.backgroundColor = UIColor.blue
peekFeedButton.frame = CGRect(x: 85,y: 0, width: 80,height: 20) as
CGRect
peekFeedButton.backgroundColor = UIColor.blue
buttonFrame.addSubview(mainFeedButton)
buttonFrame.addSubview(peekFeedButton)
navigationItem.titleView = buttonFrame
} //center bar buttons / action setup
private func setupLeftNavButton() {
navigationItem.leftBarButtonItem = UIBarButtonItem(customView:
favoriteButton)
}// left bar buttons / action setup
private func setupRightNavButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView:
moreButton)
} //right bar buttons / action setup
lazy var mainFeedButton: UIButton! = {
let button = UIButton(type: .custom) // button type
button.setTitle("Main",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showMainFeedController),
for: .touchUpInside)
return button
}() //mainFeed button connected to Feed Controller
lazy var peekFeedButton: UIButton! = {
let button = UIButton(type: .custom) //button type
button.setTitle("Spy",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 20, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showSpyFeedController),
for: .touchUpInside)
return button
}()//peekFeed button frame and action setup
lazy var favoriteButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"star").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(favoriteButton_tapped),
for: .touchUpInside)
return button
}() //favorites button frame and action setup
lazy var moreButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"more").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(moreButton_tapped),
for: .touchUpInside)
return button
}() //more button frame and action setup
func showMainFeedController() {
let navController = MainFeedController()
self.present(navController, animated: true, completion: nil)
} //mainFeed button connected to Feed Controller
func showSpyFeedController() {
let navController = SpyFeedController()
self.present(navController, animated: true, completion: nil)
}//peekFeed button connected to SpyFeedController
func favoriteButton_tapped(sender: UIButton) {
print("You touched this!")
}
func moreButton_tapped(sender: UIButton) {
print("You touched this!")
}
}
然后尝试通过setupNavigationBarItems()调用该函数,就像这样
import UIKit
class EventsController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let navbar = NavBarController()
navbar.setupNavigationBarItems()
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
}
}
我不确定这是否有效。我仍然对这一切都不熟悉。
答案 0 :(得分:0)
目前尚不清楚你期望发生什么,但这是发生了什么:
let navbar = NavBarController()
创建一个全新的NavBarController对象。
navbar.setupNavigationBarItems()
调用NavBarController对象的setupNavigationBarItems
。
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
您的代码即将结束。 navbar
是一个局部变量,因此NavBarController对象在一团烟雾中消失。结束。此对象已创建并配置为无用。
答案 1 :(得分:0)
我记得我在2015年的iOS第一个月:D,对OOP没有任何了解,我也不知道如何将数据传递到另一个屏幕或类。
无论如何,您不要在NavBarController
中创建EventsController
课程的新实例。如果您想与NavBarController
中的EventsController
对话,那么您需要一个当前有效的参考。您也可以使用delegate
(稍后搜索)。
因此,在您从EventsController
展示或展示NavBarController
之前,请将当前NavBarController
个实例传递到下一个EventsController
屏幕。但首先,你需要在EventsController
中声明一个变量,对吗? :)
在NavBarController
类中声明类型为EventsController
的变量,如下所示:
var navBarController: NavBarController!
然后在你的这段代码中,在展示或展示之前将你的自我(NavBarController
实例)传递给EventsController
类,请注意你错误地给了一个错误的名字您的EventsController
新实例,因此我将其重命名为:
func showEventsController() {
let eventsController = EventsController()
eventsController.navBarController = self // THIS :)
self.present(eventsController, animated: true, completion: nil)
}
最后,而不是:
let navbar = NavBarController()
navbar.setupNavigationBarItems()
使用你声明的变量,如下所示:
self.navBarController.navbar.setupNavigationBarItems()
希望这有帮助! :)