0

In Xcode 6, is it possible to use a .swift file as the 1st view the user will see when they open my app?

The file below is for a table view as I prefer to use swift instead of IB (too many views makes IB look messy).

    import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    let items = ["Hello 1", "Hello 2", "Hello 3"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        self.tableView = UITableView(frame:self.view.frame)
        self.tableView!.dataSource = self
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)

    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel.text = "\(self.items[indexPath.row])"
        return cell
    }
}

var ctrl = ViewController()
7
  • 1
    "use a swift as the 1st view the user will see when they open my app" Not sure what you're trying to say... But what's happening when you run your current code? Commented Dec 20, 2014 at 18:01
  • I meant "use a .swift file" Commented Dec 20, 2014 at 18:04
  • Why wouldn't you be able to use a code-based view controller as your first app view? It doesn't matter whether it's Swift or Objective-C. I don't use IB all all in any of my apps. So yes, it is possible. Commented Dec 20, 2014 at 18:06
  • How can I specify this in Xcode 6? Commented Dec 20, 2014 at 18:09
  • You do it the same way as you would have in Xcode 5. Instantiate your controller in application:didFinishLaunchingWithOptions:, and set it as the window's rootViewController. Commented Dec 20, 2014 at 18:13

1 Answer 1

1

According to Apple's View Controller Programming Guide, "When working with view controllers directly, you must write code that instantiates the view controller, configures it, and displays it." However it also states "You gain none of the benefits of storyboards, meaning you have to implement additional code to configure and display the new view controller." That being said if you still desire to do this, this is how you do it (in your app delegate):

var window: UIWindow?
var viewController: ViewController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.backgroundColor = UIColor.whiteColor()

    viewController = ViewController()
    // Any additional setup

    window?.rootViewController = viewController
    window?.makeKeyAndVisible()

    return true
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.