Prevent a view from being loaded or appearing - ios

I have a tab bar controller as the starting point of my app, where one of the tab and what happens subsequently is meant for admins only. So I was password protecting a tab. I thought of adding a little modal dialogue in the viewDidLoad function of my view controller (which by the way is a UITableViewController),
Suppose I can get the text that the user entered in the dialogue box in the variable inputTextField.
The relevant section of the code from viewDidLoad():
if inputTextField?.text != "secret" {
return
}
super.viewDidLoad()
But it does not work. Any hint appreciated. Sorry if it is too basic, I am completely new to iOS and Swift programming, so pardon my ignorance folks.

Here is a simple example. Lots of ways. I dropped two UIViews in Storyboard on the first tab's VC. The one in the back I gave a dark color to simulate the hidden secret view (secretView). Inside of the view on top (entryView) I dragged a label "Enter Passcode" and a text field (passCode). I just hid the back view unless the secret code was correct.
import UIKit
class FirstViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.passCode.delegate = self
entryView.hidden = false
secretView.hidden = true
}
#IBOutlet weak var entryView: UIView!
#IBOutlet weak var secretView: UIView!
let secretCode = "X"
#IBOutlet weak var passCode: UITextField!
func textFieldShouldReturn(textField: UITextField!) -> Bool {
if textField.text == secretCode {
entryView.hidden = true
secretView.hidden = false
} else {
self.passCode.text = "Try Again!"
}
textField.resignFirstResponder()
return true
}
}

Related

Swift UIbutton is clicked add a string and when clicked again it removes it

I'm a beginner in swift, I'm making an app in storyboard UIKit, and I need some help basically I need to set up a view controller that has buttons on it that when clicked add a string on the bottom of the VC, and if clicked again it will remove that same string. On the VC there going to be multiple buttons like this for options also on the bottom of the VC I need the label to update during the app also it should display like this for example. "Football","Basketball","Golf". It needs to be displayed just like that on the bottom with quotes and commas. I've to turn to make action buttons with a global array and put that inside each button but I can't figure out how to remove it when the button clicked again, also if you click the button again it'll add the same thing again so in the array you'll have two of the same strings. Anything would help.
P.S I need to do this in UIkit and Storyboard
You can make list of outlets to an array UIButton, handle list of actions when click into UIButton with a function. Using 'isSelected' property of UIButton to distinguish 'delete' or not.
class ViewController: UIViewController {
#IBOutlet weak var descriptionLabel: UILabel!
#IBOutlet var allButtons: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTapButton(_ sender: UIButton) {
sender.isSelected.toggle()
_updateDescription()
}
private
func _updateDescription() {
descriptionLabel.text = allButtons
.filter { $0.isSelected }
.compactMap { $0.titleLabel?.text }
.map { "\"\($0)\"" }
.joined(separator: ", ")
}
}

Connect Outlets to Tab View Controller Result in Nil Error

I've created a new window controller that hosts a tabViewController inside my app. I've added classes to the window controller, and have the same class across all the view controllers in the tab view.
I can connect buttons and give them an action and it works perfectly, however, I try to connect an outlet and attempt to change something via the outlet, it returns nil and crashes the program. For example, this exact code works in the pre-made viewController of my app, but returns:
Unexpectedly found nil while implicitly unwrapping an Optional value
when running it through the new tab view controller I created.
What's weird to me is I can use a regular view controller and the outlets connect fine, but if I want to use a tab view controller, these nil errors are happening.
I made sure that the nil was not related to grabbing the inputs for the button by printing the audio devices, and the audio devices are there and able to be printed. It seems as if the button is not there even though it is connected.
I have also tried to simply change an NSTextField color after connecting it to an outlet and this returns the same nil error.
Any idea what I might be doing wrong here? Thanks so much for the help.
class FirstLaunchViewController: NSViewController {
var FLWindow: FirstLaunchWindowController?
var selectedAudioDevice = [String]()
#IBOutlet weak var deviceListPopUpButton: NSPopUpButton!
#IBOutlet weak var visualizeButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height)
populateDeviceList()
}
#IBAction func CloseStartupGuide(_ sender: Any) {
self.view.window?.windowController?.close()
}
#IBAction func popUpDidChange(_ sender: Any) {
print("changed")
}
//grab inputs for button
fileprivate func populateDeviceList() {
deviceListPopUpButton.removeAllItems()
for device in AudioDevice.allInputDevices() {
var teststring = ""
teststring = device.uid!
print(device.name)
deviceListPopUpButton.addItem(withTitle: device.name)
deviceListPopUpButton.lastItem?.tag = Int(device.id)
selectedAudioDevice.append(device.uid!)
}
}
}
}

Adding user inputs from textfield into an array

I am trying to add users names that they have put in a textfield to a basic array. I don’t even know where to start!
The functionality basically is this:
User adds a name in text field
Name is stored in an array so it can be referenced again somewhere else.
As you can tell I’m new to Xcode. I have been taking classes all of which have failed to explain this process. Any help is good Help ! Please :):)
// my view controller so far
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var name1: UITextField!
#IBOutlet weak var name2: UITextField!
#IBOutlet weak var name3: UITextField!
var namesArray : [String] = []
override func viewDidLoad() {
super.viewDidLoad()
name1.delegate = self
name2.delegate = self
name3.delegate = self
self.namesArray.append(self.name1.text!)
self.namesArray.append(self.name2.text!)
self.namesArray.append(self.name3.text!)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
if textField.returnKeyType == UIReturnKeyType.done {
self.namesArray.append(self.name1.text!)
print(namesArray)
}
return true
}
I am going to assume you have all your logic mashed into one view controller since you are still learning and not worried about larger architecture decisions yet.
At the top of your class, you need an array variable:
// Creates an array and initializes it to be empty.
var userNames: [String] = []
I suspect you also have a button, so that when you press on the button, it grabs the text from the textfield and stores it in the array. You need to set up an IBAction method for that button, and in that method...
// Grab the text, save it in the array, and clear the text.
self.userNames.append(self.myTextField.text)
self.myTextField.text = ""
Later on, you can easily see the list of userNames by accessing self.userNames. e.g.:
let firstUser = self.userNames.first
let lastUser = self.userNames.last
let user47 = self.userNames[46] // assuming you have that many users

Save and Load text on UITextField

I am struggling for some hours at a problem. Basically, I have a simple Swift app. It goes like this. In my 1st view controller I have 3 text fields I want to fill them with information and a "Next" button.
Pressing the "Next" button will send me to a new UIViewController where I will have a "Back" button. Upon pressing the back button, I will be send back to 1st page with the 3 text fields.
What I want is: if I complete the text fields with informations, press Next and then Back, I want the text fields to be filled with that information.
I managed to move between the views with buttons, but I can't save the information. Can you provide me a little help?
#IBOutlet weak var txtb1: UITextField!
#IBOutlet weak var txtb2: UITextField!
#IBOutlet weak var txtb3: UITextField!
#IBAction func next(sender: AnyObject) {
text1 = self.txtb1.text!
text2 = self.txtb2.text!
text3 = self.txtb3.text! }
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (!text1)
{
txtb1.text = text1
}
}
If you are wanting to pass variables to different views you will have to use the prepareForSegue function before you segue into that view controller.
This allows the variables to be accessed by the view controller.
Override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
if segue.identifier == "segue_name"
{
let theDestination = segue.destinationViewController as! newViewController
theDestination.variable = "testing"
theDestination.variable2 = "testing2"
}
}
Then inside your second view controller you will have to declare variable and variable2 as shown:
var variable:String!
var variable2:String!
These will then be able to be used in the second view controller inside the viewDidLoad method.
Ok. I managed to fix a little of the problem, that button wasn't linked accordingly so the function was not executing. Here it is the code:
var text1 = ""
#IBOutlet weak var txtb1: UITextField!
#IBAction func next(sender: AnyObject) {
text1 = self.txtb1.text! // text1 = "I am taking value"
}
// when i press the back button
override func viewDidLoad() {
super.viewDidLoad()
if (!numarInmatriculare.isEmpty)
{
txtb1.text = text1 //here text1 = Null. the value is not saved
}
}
I want the value to be saved and displayed back on text field.
I also tried to use static varibiles but I am prompet with error.
static var numar:String = ""
numar = text1
//static member 'numar'cannot be used on instance of type 'viewcontroller'
You no need to do anything for this case, once text field filed then you clicking next button you wrote some code below i shown
text1 = self.txtb1.text!
text2 = self.txtb2.text!
text3 = self.txtb3.text!
Please first you remove the code,Once data filled in text filed, its automatically retain, after you clicking next button then come back it automatically it will retain, when your current view is poping then only your data will deallocate.

how to hide/show a button in swift

I'm trying to have an if statement that will make a button hidden when a label displays a certain status, and appears when the label says something else. The name of the label is Status, and when it shows "Closed", I want it hidden, and when it shows "Open", it will appear.
var query3 = PFQuery(className:"Status_of_game")
query3.findObjectsInBackgroundWithBlock{
(namelist3: [AnyObject]!, error : NSError!) -> Void in
for list3 in namelist3 {
var output = list3["StatusType"] as String
self.Status.text = output
println(output)
if self.Status.text == "Closed"
{
Purchase().enable = false
}
}
}
As #LAmasse says, you want to use button.hidden = true. button.hidden was renamed to button.isHidden in Swift 3
The code you posted doesn't make sense.
if self.Status.text == "Closed"
{
Purchase().enable = false
}
What is Purchase? From the capitalized name, it seems to be a class. If so, the expression Purchase() is likely creating a new instance of the Purchase class, which makes no sense. Why are you making a function call? If that is creating a new Purchase object then that code is pointless. (You would create a new object inside the if statement that would be discarded on the very next line since you don't keep a strong reference to it.)
You want to set up an IBOutlet for your button and connect it in Interface Builder.
The declaration might look like this:
Class MyViewController: UIViewController
{
#IBOutlet weak var theButton: UIButton!
//The rest of your view controller's code goes here
}
If the outlet is connected to your button, there should be a filled-in circle to the left of the line of code. It looks like this:
And then your code to show/hide the button might look like this:
func showQueryResults
{
var query3 = PFQuery(className:"Status_of_game")
query3.findObjectsInBackgroundWithBlock()
{
(namelist3: [AnyObject]!, error : NSError!) -> Void in
for list3 in namelist3
{
var output = list3["StatusType"] as String
self.Status.text = output
println(output)
if output == "Closed"
{
theButton.isHidden = false //changed to isHidden for Swift 3
}
}
}
}
It isn't clear to me why you'd loop though all of the results from your query and and show the button if the "StatusType" of any of the results is == "Closed".
Finally, I'm not very familiar with parse. If the completion block for the findObjectsInBackgroundWithBlock method doesn't get called on the main thread you will have to change that code to make the UI updates on the main thread.
EDIT:
I've since learned that Parse executes its completion handlers on the main thread, so you don't need to worry about UI calls from Parse completion handlers.
SWIFT 3
I created an
IBOutlet: loadingBDLogo
To Show:
loadingBDLogo.isHidden = false
To Hide:
self.loadingBDLogo.isHidden = true
The sample code for hiding a button in Swift:
import UIKit
class ViewController: UIViewController {
// Create outlet for both the button
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
//Set button2 hidden at start
button2.isHidden = true
}
//Here is the action when you press button1 which is visible
#IBAction func button1(sender: AnyObject) {
//Make button2 Visible
button2.isHidden = false
}
}
And
You have to make the UIButton a property of the class if you want to keep a reference to it. Then you can access it using self.takePhotoButton.
To hide a button, use button.hidden = true
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/cl/UIView

Resources