我是IOS
和IBAction
编程的新手。我所做的就是在ViewController SelectionViewVC中使用子视图调用yearSelectionVC。
我在SelectionViewVC中所做的就是使用UISegmentedControl将子视图更改为不同的ViewController。但是,我试图通过界面构建器在子视图上添加一些按钮,并在按下按钮时使用IBAction
触发一些代码,根本不起作用。我已经检查了子视图中的所有按钮都链接到相同的class SelectionViewVC: UIViewController {
var requestd_date:Date = Date()
@IBOutlet weak var requested_date_label: UILabel!
@IBOutlet weak var viewContainer: UIView!
var views:[UIView]!
static var selected_year = 0
static var selected_month = 0
static var selected_day = 0
static var selected_hour = 0
static var selected_minute = 0
override func viewDidLoad() {
super.viewDidLoad();
updateCurrentDate()
SelectionViewVC.selected_year = current_year
SelectionViewVC.selected_month = current_month
SelectionViewVC.selected_day = current_day
SelectionViewVC.selected_hour = current_hour
SelectionViewVC.selected_minute = current_minute
views = [UIView]()
views.append(yearSelectionVC().view)
views.append(monthSelectionVC().view)
views.append(dateSelectionVC().view)
views.append(hourSelectionVC().view)
views.append(minuteSelectionVC().view)
for view in views {
viewContainer.addSubview(view)
}
viewContainer.bringSubview(toFront: views[0])
let rightBarButton = UIBarButtonItem(title: "Submit", style: UIBarButtonItemStyle.plain, target: self, action: #selector(SelectionViewVC.submitButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = rightBarButton
let request_date_string = dateFormatter.string(from: requestd_date)
requested_date_label.text = "Date Requested to input: \(request_date_string)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func submitButtonTapped(_ sender:UIBarButtonItem){
self.navigationController?.popViewController(animated: true)
}
@IBAction func switchViewAction(_ sender: UISegmentedControl) {
self.viewContainer.bringSubview(toFront: views[sender.selectedSegmentIndex])
}
}
class yearSelectionVC: UIViewController {
@IBOutlet weak var selected_date_label: UILabel!
@IBOutlet weak var year1: UIButton!
@IBOutlet weak var year2: UIButton!
@IBOutlet weak var year3: UIButton!
@IBOutlet weak var year4: UIButton!
@IBOutlet weak var year5: UIButton!
@IBOutlet weak var year6: UIButton!
@IBOutlet weak var year7: UIButton!
@IBOutlet weak var year8: UIButton!
@IBOutlet weak var year9: UIButton!
@IBOutlet weak var year10: UIButton!
@IBOutlet weak var year11: UIButton!
@IBOutlet weak var year12: UIButton!
@IBOutlet weak var year13: UIButton!
@IBOutlet weak var year14: UIButton!
@IBOutlet weak var year15: UIButton!
@IBOutlet weak var year16: UIButton!
var yearButtonList:[UIButton]!
var current_temp_year = 0
var originalButtonColor:UIColor!
override func viewDidLoad() {
super.viewDidLoad()
yearButtonList = [UIButton]()
yearButtonList.append(year1)
yearButtonList.append(year2)
yearButtonList.append(year3)
yearButtonList.append(year4)
yearButtonList.append(year5)
yearButtonList.append(year6)
yearButtonList.append(year7)
yearButtonList.append(year8)
yearButtonList.append(year9)
yearButtonList.append(year10)
yearButtonList.append(year11)
yearButtonList.append(year12)
yearButtonList.append(year13)
yearButtonList.append(year14)
yearButtonList.append(year15)
yearButtonList.append(year16)
current_temp_year = SelectionViewVC.selected_year
updateSelectedDateText()
originalButtonColor = yearButtonList[15].titleLabel?.textColor
yearButtonList[0].setTitleColor(.red, for: .normal)
for year in current_temp_year...current_temp_year+15 {
yearButtonList[year-current_temp_year].setTitle(String(describing:year), for: .normal)
yearButtonList[year-current_temp_year].tag = year-current_temp_year
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateSelectedDateText() {
selected_date_label.text = "Selected Date: \(SelectionViewVC.selected_year)/\(month_degit_list[SelectionViewVC.selected_month-1])/\(day_list[SelectionViewVC.selected_day-1]) \(hour_list[SelectionViewVC.selected_hour]):\(minute_list[SelectionViewVC.selected_minute])"
}
@IBAction func yearButtonTapped(_ sender: UIButton) {
sender.setTitle("done", for: .normal)
}
}
代码,我该怎么做才能使这些按钮工作?谢谢你的帮助!
注意:子视图是在XIB文件中创建的
^([^(]+?)\s*\(\s*([^)]*?)\s*\)\s*(.*)