Prevent automatically create more on ios tabbar - ios

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")
}
}

Related

When I press iPhone home button, sleep/power button or terminate my app, and rerun it again, The TestButtonAction don't print

I made a simple code to clarify my problem. When I run my app on my iPhone it works properly, but when I press home button - either on simulator or my real iPhone - and press the app icon again, the app runs, but the app's button "TestButtonAction don't work. Same problem when I press power/sleep button and turn it on again.
same problem either when I duple click home button and terminate my app by sliding it's window away, and rerun it again the button don't work or print "Right Image" in the Console
Here is my code, Thanks.
//
// ViewController.swift
// TestingHomeButton
//
// Created by Samuel Oncy on 4/25/18.
// Copyright © 2018 Samuel Oncy. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
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.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
if(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView")){
print("Right Image")
}
}
}
Simply Make UIImage global Variable, If you want to compare in If.
Other ways can also be to solve same problem - Tag is right one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var TestButtonOutlet: UIButton!
var image = UIImage (named: "NatureView")
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.
}
#IBAction func TestButtonAction(_ sender: UIButton) {
// print(TestButtonOutlet.imageView?.image == UIImage(named:"NatureView"))
if (TestButtonOutlet.currentImage == image) {
print(TestButtonOutlet.currentImage)
}
}
}
To check the current image of button, use the currentImage property as follows:-
#IBAction func TestButtonAction(_ sender: UIButton) {
if sender.currentImage.isEqual(UIImage(named: "NatureView")) {
print("Right Image")
}
}

How to actually catch when back button is pressed?

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)

how to acces a variable in another class and change its value

I am new to Xcode and Swift so I don't know much about how it all works, but I am trying to make a pop-up view. I want a small view to pop up when I click a button. The view is a View Container (I don't know if that is the best way to do this so if not please tell me a better way to do this) and it starts out hidden then when I click a button it becomes visible. This View Container also has a button that if clicked, it will make the view hidden again.
Here is the code:
import UIKit
class ViewController: UIViewController {
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.
}
#IBOutlet weak var popUpView: UIView!
#IBAction func startButton(sender: UIButton) {
popUpView.hidden = false
}
}
import UIKit
class PopUpViewController: 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.
}
override func prepareForSegue(segue:UIStoryboardSegue,
sender:AnyObject?)
{
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
#IBAction func backButton(sender: UIButton) {
ViewController().popUpView.hidden = true
}
}
When I run the app it starts fine because the start button is there and when I click it the pop up shows up but when I click the back button it gives me an error which says that in the console
Unknown class MKMapView in Interface Builder file.
fatal error: unexpectedly found nil while unwrapping an Optional value
and in line 31 ViewControler().popUpView.hidden = true
it says Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP, subcode=0x0)
Can someone help. Thanks
Access popUpView variable from didPrepareForSeque method (this method gets called automatically when you segue to another view). Problem is that if you try to set value to soon (meaning, that button is not drawn on view), you will get nil error. Here is a little workaround. You use temporary variable (tmpValue) to store state of your button (to be hidden or not), so when viewDidLoad, you method will read this value and set button to hidden state as you intended.
In ViewController class declare temporary variable (must be optional):
var tmpValu:Bool?
Then in your PopUpViewController class remove this line from backButton action:
ViewController().popUpView.hidden = true
Instead, you will use prepareForSegue method, like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destinationViewController = segue.destinationViewController as! ViewController
destinationViewController.tmpValu = true
}
Now, back in ViewController class in viewDidLoad add this code:
override func viewDidLoad() {
super.viewDidLoad()
if let value = tmpValu {
popUpView.hidden = value
}
}

SWIFT function with parameters in viewDidLoad

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.

UIBarButtonItem with customView (UISwitch) disappears shortly after creating the toolbar (even though it's a really simple code!)

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.

Resources