Amraj
Amraj

Reputation: 151

Xcode 5 - iOS - How to Create a View Controller in Xcode without Storyboard or XIB and how to configure it into AppDelegate as the root view

Right I had taken a break from iOS dev and I have come back into the mix and am having issues with the following:

I want to create a View Controller with an embedded UIWebView programatically without any xib or storyboard dependancy but I can't remember for love nor money how to get the AppDelegate to show my View Controller.

Under all the documentation All I can find is placing the below under my AppDelegate

self.window = [[ViewController alloc] initWithNibName:@"blah" bundle: nil];

But As I don't have any xib's associated to my ViewController files this would be useless.

Could someone please set me back on the right path.

Thanks

Upvotes: 1

Views: 803

Answers (2)

Lesi Funmilade
Lesi Funmilade

Reputation: 1

class ViewController: UIViewController, UIPageViewControllerDataSource, CLLocationManagerDelegate,UITextFieldDelegate, UITableViewDataSource,UITableViewDelegate { //create a location manager let locationManager = CLLocationManager()

//menu button
@IBOutlet weak var menu: UIBarButtonItem!

@IBOutlet weak var locationTextField: UITextField!{
    didSet {
        locationTextField.delegate = self
        locationTextField.text = ""
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return globalSearchText.searchArrayLocation.count
}

func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    locationTextField.text = globalSearchText.searchArrayLocation[indexPath.row]
}

Upvotes: 0

Matteo Gobbi
Matteo Gobbi

Reputation: 17707

In AppDelegate.m:

CustomViewController *rootViewController = [CustomViewController new];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

In CustomViewController.m override:

- (void)loadView {
    //Configure your view
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //other stuff
    [self.view setBackgroundColor:[UIColor redColor]];

    UIButton *button = /* alloc init etc. */
    [self.view addSubview:button];
}

Upvotes: 3

Related Questions