Xcode 6 interface builder, cannot link storyboard window to code - xcode

I am a relatively new developer working with swift in Xcode 6.1 beta. I am trying to make a simple application that has 2 text fields: 1 for first name, 1 for last name. When the submit button is pressed, a new window opens and displays the full name.
I have successfully gotten the application to work on a single window, with the result appearing in a third non-editable text field.
However, when I add the second view controller to the storyboard for the new window, make it launch when the button is pushed, and drag the result field into it, the application does not work.
I tried re-linking the field to my ViewController.swift (where all my code is), but it does not show up on automatic. When I open it via manual, I cannot link to the file.
original window:
http://i.stack.imgur.com/C0xui.png
new window:
http://i.stack.imgur.com/XdZ2l.png
Viewcontroller.swift:
import Cocoa
class ViewController: NSViewController {
#IBOutlet var window: NSView!
#IBOutlet weak var first: NSTextField!
#IBOutlet weak var last: NSTextField!
#IBOutlet weak var output: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func btn(sender: AnyObject) {
var first = self.first.stringValue
var last = self.last.stringValue
if first.isEmpty {
self.output.stringValue = ""
}
else {
self.output.stringValue = "Hello, \(self.first.stringValue) \(self.last.stringValue)!"
}
}
override var representedObject: AnyObject? {
didSet {
}
}
}

You have to show assistant window in the menu
View > Assistant Editor > Show Assistant Editor

Related

Assistant Editor shows different "ViewController.swift" file than the one that Main Editor shows?

The source code shown in the Assistant Editor for "ViewController.swift" is different than source code shown in Main Editor for "ViewController.swift".
"ViewController.swift" in Main Editor:
// ViewController.swift
// FoodTracker
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var mealNameLabel: UILabel!
#IBOutlet weak var mainButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.delegate = self
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(textField: UITextField) {
mealNameLabel.text = textField.text
}
// MARK: Actions
#IBAction func setDefaultLabelText(sender: UIButton) {
mealNameLabel.text = "DEFAULT text"
// mainButton.tintColor = UIColor.darkTextColor()
}
}
"ViewController.swift" in Assistant Editor:
//
// ViewController.swift
// FoodTracker
import UIKit
internal class ViewController : UIViewController, UITextFieldDelegate {
#IBOutlet weak internal var nameTextField: UITextField!
#IBOutlet weak internal var mealNameLabel: UILabel!
#IBOutlet weak var mainButton: UIButton!
override internal func viewDidLoad()
internal func textFieldShouldReturn(textField: UITextField) -> <<error type>>
internal func textFieldDidEndEditing(textField: UITextField) -> <<error type>>
#IBAction internal func setDefaultLabelText(sender: UIButton) -> <<error type>>
}
So, those are completely different different files, but have the same name. The one in the Assistant Editor is the interface while the file shown in the Main Editor is the implementation of the interface, right?
That seems a little weird, but the interface and implementing class have the same name? When I'm working in Xcode I need to be aware that sometimes two files can (often?) have the same name?
I had the same problem. I couldn't figure out why it was showing this "internal class" file.
I managed to get it to display the correct associated file. To do this, click the associated editor icon, the two circles. Then in the window that appears, click the '+'. The new window that appears should have the right code in it. Then close the old window and you should be left with the right one. The rest of the project appears to work be fixed now.
I found this in Xcode 9.2, Swift 4, but it may be in other versions.
In Xcode, with the option key down, hover your mouse over 'func' or another scope-describing keyword to reveal the scope with the blue bracket.
Then do a two finger tap on the trackpad to automatically open an Assistant Editor at the same point in the code. I'm frequently going somewhere else in my code but want to have a window open that stays where I just was. This trick does just that.
(Make sure your track pad is set so a 'Secondary click' is a tap with two fingers.)
(Swift 4.2) I just had the same problem. Whenever I was in the assistant editor, my ViewController was labeled internal and I couldn't make any edits to it. The above solutions didn't work for me, but they put me on the right trail. Apparently, I was in some kind of duplicate file and not in the ViewController I needed. Correcting the issue was simply a matter of navigating to the correct file via the controls at the top of the assistant editor.
I have seen what could be a solution at the video youtube.com/watch?v=wPAUKhlmW1M at time-position 1:31 - please correct if I'm wrong

NSPopupButton has nil value

I am building an OS X desktop app that allows a user to select an item from a dropdown. I am trying to create an NSPopupButton menu like the response to this question, which is also very similar to this tutorial, but when I build and run in Xcode, I get an EXC_BAD_INSTRUCTION error and the NSPopupButton evaluates to nil in the debugger. Did I miss a step initializing the menu? I also have a text input, but it works just fine. My code:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var textInput: NSTextField!
#IBOutlet weak var myMenu: NSPopUpButton!
// other stuff here for processing textInput
#IBAction func selectFromMyMenu(sender: NSPopUpButton) {
let selection = myMenu.titleOfSelectedItem
if selection == "Second Option" {
// do something
} else {
// do something else - first option is default
}
}
func setupMyMenu() {
let menuItems = ["First Option", "Second Option"]
myMenu.removeAllItems()
myMenu.addItemsWithTitles(menuItems)
myMenu.selectItemAtIndex(0)
}
override func viewDidLoad() {
super.viewDidLoad()
setupMyMenu()
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
Try reconnecting your button from the storyboard to your ViewController code.

Dismissing View Controller for osx

I'm trying to learn swift code for mac OSX but there isn't much tutorials for it as ios. and i have been struggling already with closing or dismissing the view controller when i launch through a button another connected view controller
class ViewController: NSViewController {
#IBOutlet weak var username: NSTextField!
#IBOutlet weak var password: NSTextField!
#IBAction func login(sender: AnyObject) {
}
#IBAction func signUp(sender: AnyObject) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
I tried to add [self dismissModalViewControllerAnimated:YES]; to dismiss it but it doesn't work it only shows errors. if anybody could point me to somewhere i can get more information or what i'm doing wrong?
Use
dismissViewController(self)
to dismiss the presented view controller.
About dismissViewController: from the NSViewController docs:
Dismisses a presented view controller, using the same animator that presented it.
and
In OS X, this is the universal way to dismiss a view controller, no matter how it was presented.

Swift Adding Image to a Button

I want to add an image to a button, and when it is clicked, i want it to change. I'm very new to Swift so I know very little.
I have it like this:
//
// ViewController.swift
// Tip Calculator
//
// Created by ios4 on 10/16/15.
// Copyright (c) 2015 ios4. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var tipAmount: UILabel!
#IBOutlet weak var userBillTextField: UITextField!
#IBOutlet weak var tenOutlet: UIButton!
override func viewDidLoad()
{
tipAmount.text = " "
// var billDouble = (userBillTextField.text as NSString).doubleValue
super.viewDidLoad()
}
#IBAction func calculateButton(sender: UIButton)
{
var billDouble = NSString(string: userBillTextField.text).doubleValue
tipAmount.text = String( stringInterpolationSegment: billDouble * 0.15)
}
#IBAction func tenButton(sender: UIButton)
{
}
and I want the outlet called ten outlet to change to a different image called 10_selected_image from 10_unselected_image.
Any help?
Here are my instructions for this : https://s3.amazonaws.com/mmhighschool/MasterDocs/TipCalculator/TipCalculatorAppDirections.pdf
I'm currently working on stretch 3
You should be able to use set image for control state in your button action
button.setImage(image: UIImage(named: <image name>), forState: UIControlState.Normal)
If you need more info about how to make a "toggle button" this thread has a good answer to that
How to use a UIButton as a toggle switch
For me in Swift 5 is working like:
boton1.setBackgroundImage(UIImage(named: "image Name"), for: UIControl.State.normal)

Setting Label text in swift

My first question here. I'm making the jump from Delphi and Pascal to Xcode and Swift and feeling totally overwhelmed by the change.
I'm simply attempting to change the text of a label when a button is clicked. Here is my code from ViewController.swift
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var Label1: NSTextField!
#IBOutlet weak var ChangeText: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func ButtonPressed(sender: AnyObject) {
Label1.stringValue = "Hello World"
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
When I run the application as soon as I click the button the application hangs.
I've also tried exchanging stringValue with text but NSTextField does not use this. I can't seem to make the label show up as UITextField.
What am I doing wrong?

Resources