Category: User Interface

December 21st, 2016 by admin

If you’re creating a macOS program using storyboards, you may need to know the size of a window. To get this information, you just need to follow two steps:

  • Create an IBOutlet that represents your window. This IBOutlet should be of the type NSView
  • Use the frame.width and frame.height properties to get the width and height of the window defined by your IBOutlet

To create an IBOutlet for a window, just opens he Assistant Editor, move the mouse pointer over the window, and Control-drag to the Swift class file linked to your window such as ViewController.swift. Then release the Control key and the left mouse button to create an IBOutlet.

You can use any name for your IBOutlet, but whatever name you choose will now represent your window such as:

@IBOutlet var myWindow: NSView!

Assuming you named your window IBOutlet myWindow, you’ll be able to access the frame.width and frame.height properties like this:

myWindow.frame.height
myWindow.frame.width

If you put the following two lines of code in the viewDidLoad function that runs as soon as a window appears, you’ll see the defined width and height of the window.

print ("Window height = \(myWindow.frame.height)")
print ("Window width = \(myWindow.frame.width)")

Posted in Algorithms, User Interface

May 5th, 2016 by admin

if you’ve ever used the appointment feature of the iOS calendar, you’ve seen a spinning dial that lets you choose among multiple options such as the names of different months and days. A PickerView simply displays options that the user can choose. To fill a PickerView with data, you can use an array and place that PickerView on the user interface.

Next, you have to add the UIPickerViewDelegate and UIPickerViewDataSource to the class linked to the view (user interface) like this:

                                        // 1. Add UIPickerViewDelegate and UIPickerViewDataSource to the class
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

Then you have to get data to place in the PickerView, which can be an array like this:

    // 2. Create an array of data to display in the picker view
    var pickerArrayItems = ["Cat", "Dog", "Hamster", "Goldfish", "Parrot", "Lizard"]

You also need to create an IBOutlet for your PickerView so you can refer to it in Swift code like this:

    // 3. Create an IBOutlet for the picker view on the user interface
    @IBOutlet weak var pickerViewUI: UIPickerView!

Once you’ve created an IBOutlet for your PickerView, you can then set its delegate to the view controller Swift file like this:

        // 4. Set the delegate of the picker view control to the view controller
        pickerViewUI.delegate = self

Now you need to define the number of parts or components in your PickerView. The simplest PickerView displays just one type of data so you can define just 1 using the numberOfComponentsInPickerView function:

    // 5. Define the number of parts or components of the picker view
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }

You must also define how many items to display in the PickerView. If your data is an array, you can just count the number of items in the array using the pickerView function:

    // 6. Define the number of items to appear in the picker view
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerArrayItems.count
    }

Finally, you need to actually fill the PickerView with your data using another pickerView function:

    // 7. Define the title of each row of the picker view with the array items (Step 2 above)
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerArrayItems[row]
    }

Your entire ViewController.swift file can look like this when linked to a view (user interface) that contains a picker view control:

import UIKit

                                        // 1. Add UIPickerViewDelegate and UIPickerViewDataSource to the class
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    // 2. Create an array of data to display in the picker view
    var pickerArrayItems = ["Cat", "Dog", "Hamster", "Goldfish", "Parrot", "Lizard"]

    // 3. Create an IBOutlet for the picker view on the user interface
    @IBOutlet weak var pickerViewUI: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 4. Set the delegate of the picker view control to the view controller
        pickerViewUI.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // 5. Define the number of parts or components of the picker view
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    // 6. Define the number of items to appear in the picker view
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerArrayItems.count
    }

    // 7. Define the title of each row of the picker view with the array items (Step 2 above)
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerArrayItems[row]
    }
    
}

Posted in iOS, User Interface

May 2nd, 2016 by admin

Your app can display a web page by using the Web View object on your user interface and then by writing three lines of code. So first, place a Web View on your user interface and give it a unique name such as webView, although the exact name isn’t important.

Next, create a variable or constant using any name you wish. Then set this variable/constant to a string that defines the complete web site address like this:

        // The web address must be https:// and not just http://
        let showURL = NSURL(string: "https://www.apple.com")

The only restriction is that the web site address must use https:// and not just plain http:// or else it won’t work.

After creating a variable/constant to represent the web site address, you need to create a second variable/constant to request the web site address, known as a URL:

        let requestThis = NSURLRequest(URL: showURL!)

Finally you need to use the loadRequest method on your Web View to request a particular web site domain address:

        webView.loadRequest(requestThis)

So the complete code to work with a Web View object on your user interface looks like this:

        // The web address must be https:// and not just http://
        let showURL = NSURL(string: "https://www.apple.com")
        let requestThis = NSURLRequest(URL: showURL!)
        webView.loadRequest(requestThis)

Just paste a different string for the web site address and you’ll be able to display a different web site. Remember, make sure the web site address uses https:// and also make sure the web site is optimized for mobile devices so it adapts to the smaller screen of an iPhone or iPad.

Posted in Programming, User Interface, Xcode

HTML Snippets Powered By : XYZScripts.com