There are many answers but they don't answer the question. For example, I put the following code in:
override func viewWillDisappear(animated: Bool) {
if (self.navigationController!.viewControllers.indexOf(self)==nil) {
print("back button pressed\n")
}
super.viewWillDisappear(animated)
}
But 'back button pressed' gets printed to the console even when the back button has not been pressed. The scene has buttons that return to the previous scene using unwind segues, and these cause 'back button pressed' to be printed. I need to execute code only if the back button has been pressed.
Edit for Muneeba:
import UIKit
class NewViewController: UIViewController, UINavigationBarDelegate {
func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem) {
performSegueWithIdentifier("returnToStepOne", sender: self)
delegate.backFromNewViewController()
}
override func viewDidLoad() {
super.viewDidLoad()
// self.navigationController.navigationBar.delegate = self;
navigationController?.navigationBar.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try with following UINavigationBarDelegate.
func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)
Related
Ok so I am trying to make it when the user flips the cheat mode switch it will disable the answerButton. I did try to set the button to disable near the bottom however it does not appear to do anything. I added the print("") to see if it would print in the console however nothing was printed. I a unsure what is wrong here.
I have pasted my code below.
import Foundation
import UIKit
class SettingsController: UITableViewController {
#IBOutlet weak var cheatSwitch: UISwitch!
override func viewDidLoad() {
super.viewDidLoad()
cheatSwitch.isOn = UserDefaults.standard.bool(forKey: "switchState")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveSettings(_ sender: UISwitch) {
UserDefaults.standard.set(cheatSwitch.isOn, forKey: "switchState")
UserDefaults.standard.synchronize()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let mainView : ViewController = segue.destination as! ViewController
if cheatSwitch.isOn == true {
print("turn off button")
mainView.btnAnswer.isEnabled = false;
}
else {
print("turn on button")
mainView.btnAnswer.isEnabled = true;
}
}
}
You are saving the value UISwitch's state in UserDefaults so you can use its value in ViewController's method viewDidApper.
override func viewDidAppear(_ animated: Bool) {
self.btnAnswer.isEnabled = !UserDefaults.standard.bool(forKey: "switchState")
}
Note: There is no need to add prepareForSegue in your SettingsController because it will never called when you press back button of NavigationController so consider to remove it.
I use https://github.com/itsKaynine/SwiftRaisedTab for center rasedbutton.
I test with storyboard, but something strange.
this is my app's view
click third tab
segue linked with show, and I clicked cancel for exit
tab bar is changed. why appears more button?
this is my storyboard
I want just back and forth tab to detail
I don't want more button. tab bar buttons should be fixed 4 items
This is https://github.com/itsKaynine/SwiftRaisedTab 's errors
everytime called insertEmptyItem and addRaisedButton.
import UIKit
import SwiftRaisedTab
class ViewController: RaisedTabBarController {
var isActivated: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if(!isActivated) {
isActivated = true
// Insert empty tab item at center index. In this case we have 5 tabs.
self.insertEmptyTabItem("", atIndex: 2)
// Raise the center button with image
let img = UIImage(named: "icon_camera")
self.addRaisedButton(img, highlightImage: nil)
}
}
// Handler for raised button
override func onRaisedButton(sender: UIButton!) {
super.onRaisedButton(sender)
println("Raised button tapped")
}
}
I am creating an iOS application in which I want to perform an assignment action in button press before running the method prepareForSegue.
I created all controls using Main Story board.
The order of execution for some buttons is
button press action -> prepareForSegue
but for some buttons it is
prepareForSegue-> button press action
How to change the order for second set of buttons?
Here is the code I am using:
import UIKit
class SummaryViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var button_prsd = "none"
#IBAction func people_insights(sender: AnyObject) {
button_prsd = "people_insights"
}
#IBAction func industry_research(sender: AnyObject) {
button_prsd = "industry_research"
}
#IBAction func holidays_events(sender: AnyObject) {
button_prsd = "holidays_events"
}
#IBAction func monthly_spotlights(sender: AnyObject) {
button_prsd = "monthly_spotlights"
}
#IBAction func blog(sender: AnyObject) {
button_prsd = "blog"
}
#IBAction func about_us(sender: AnyObject) {
button_prsd = "about_us"
print("button press about us executed")
}
#IBAction func settings(sender: AnyObject) {
button_prsd="settings"
print("button press settings executed")
}
#IBAction func quiz(sender: AnyObject) {
button_prsd="quiz"
print("button press quiz executed")
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let next_view = segue.destinationViewController
if(next_view is DetailViewController)
{
let det_view = next_view as! DetailViewController
det_view.link = button_prsd
print("segue executed")
} else if(next_view is DetailTableViewController)
{
let det_view = next_view as! DetailTableViewController
print("segue executed")
}
}
}
I figured out the problem, when give then code in this way, the execution is just alphabetical order, all the other methods were getting executed before prepareForSegue because the methods come above in alphabetical order
When I renamed the quiz and settings methods as a_quiz and a_settings, it worked
I have an array of buttons and at the beginning of the program I want the user to pick one button and then it'll change its background.
I've written a switch-statement but I don't know how can I implement it into override func viewDidLoad(), namely, I have no idea what should I write as a parameter instead of UIButton
class ViewController: UIViewController {
#IBOutlet var cardsArray: Array<UIButton> = []
override func viewDidLoad() {
super.viewDidLoad()
self.playerMove(sender: UIButton)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playerMove(sender: UIButton) {
switch (sender) {
case self.cardsArray[0]:
self.cardPressedAll(0)
case self.cardsArray[1]:
self.cardPressedAll(1)
case self.cardsArray[2]:
self.cardPressedAll(2)
default: break
}
}
func cardPressedAll (cardNumber: Int) {
self.cardsArray[cardNumber].enabled = false
self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
}
}
Instead of calling 'self.playerMove(sender: UIButton)' in your view did load you need to connect your buttons to your IBAction using storyboard.
I try to create a toolbar with a UIBarButtonItem with a customView (UISwitch). This is done by my function "createtoolbar()".
On viewDidLoad() the toolbar is created properly.
BUT: Creating the toolbar by pressing a Button, the UISwitch disappears approx. 0.1 seconds later.
Hope someone can help me! :)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
createtoolbar()
}
#IBOutlet var bottomBar: UIToolbar!
let alarmSwitch = UISwitch()
func createtoolbar() {
alarmSwitch.on = true
let alarmSwitchBarButton = UIBarButtonItem(customView: alarmSwitch)
var toolbarbuttons = [alarmSwitchBarButton]
bottomBar.setItems(toolbarbuttons, animated: true)
}
#IBAction func createtoolbarButtonPressed(sender: AnyObject) {
createtoolbar()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It's unclear what you're trying to do. You're creating the bar button item in code but adding a switch that's an IBOutlet? You can't do that -- if your switch actually is an IBOutlet, then it will already be the subview of some other view, and you can't use it in you bar button. If it's not an IBOutlet (which to shouldn't be), then you need to create the switch in code.