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()
rmaddy
  • 314,917
  • 42
  • 532
  • 579
wbk727
  • 8,017
  • 12
  • 61
  • 125
  • 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? – Lyndsey Scott Dec 20 '14 at 18:01
  • I meant "use a .swift file" – wbk727 Dec 20 '14 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. – rmaddy Dec 20 '14 at 18:06
  • How can I specify this in Xcode 6? – wbk727 Dec 20 '14 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. – rdelmar Dec 20 '14 at 18:13
  • I'm a beginner + never used Xcode 5. – wbk727 Dec 20 '14 at 18:18
  • Maybe this will help guide you in the right direction: http://stackoverflow.com/q/24046898/2274694 – Lyndsey Scott Dec 20 '14 at 18:22

1 Answers1

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
}
Ian
  • 12,538
  • 5
  • 43
  • 62