Why can’t I call the default super.init() on UIViewController in Swift?

The designated initialiser for UIViewController is initWithNibName:bundle:. You should be calling that instead. See http://www.bignerdranch.com/blog/real-iphone-crap-2-initwithnibnamebundle-is-the-designated-initializer-of-uiviewcontroller/ If you don’t have a nib, pass in nil for the nibName (bundle is optional too). Then you could construct a custom view in loadView or by adding subviews to self.view in viewDidLoad, same as you used to.

How does View Controller Containment work in iOS 5?

The UIViewController docs are pretty clear on when and when not to call willMove/didMove methods. Check out the “Implementing a Container View Controller” documentation. The docs say, that if you do not override addChildViewController, you do not have to call willMoveToParentViewController: method. However you do need to call the didMoveToParentViewController: method after the transition is … Read more

presentViewController and displaying navigation bar

It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way: Rather … Read more

Removing viewcontrollers from navigation stack

Use this code and enjoy: NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers]; // [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack. [navigationArray removeObjectAtIndex: 2]; // You can pass your index here self.navigationController.viewControllers = navigationArray; [navigationArray release]; Hope this will help you. Edit: Swift Code guard let navigationController = self.navigationController … Read more

How do I make a custom initializer for a UIViewController subclass in Swift?

class ViewController: UIViewController { var imageURL: NSURL? // this is a convenient way to create this view controller without a imageURL convenience init() { self.init(imageURL: nil) } init(imageURL: NSURL?) { self.imageURL = imageURL super.init(nibName: nil, bundle: nil) } // if this view controller is loaded from a storyboard, imageURL will be nil required init?(coder aDecoder: … Read more

UIView touch event in controller

You will have to add it through code. First, create the view and add it to the hierarchy: var myView = UIView(frame: CGRectMake(100, 100, 100, 100)) self.view.addSubview(myView) After that initialize gesture recognizer. Until Swift 2: let gesture = UITapGestureRecognizer(target: self, action: “someAction:”) After Swift 2: let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:))) Then … Read more

Storyboard – refer to ViewController in AppDelegate

Have a look at the documentation for -[UIStoryboard instantiateViewControllerWithIdentifier:]. This allows you to instantiate a view controller from your storyboard using the identifier that you set in the IB Attributes Inspector: EDITED to add example code: UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@”MainStoryboard” bundle: nil]; MyViewController *controller = (MyViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @”<Controller ID>”];

How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?

Swift example Updated for Xcode 10 and Swift 4 (and reportedly still works for Xcode 12.4/Swift 5) Here is a basic walk through. I originally learned a lot of this from watching this Youtube video series. Later I updated my answer based on this article. Add custom view files The following two files will form … Read more

Dismissing a Presented View Controller

I think Apple are covering their backs a little here for a potentially kludgy piece of API. [self dismissViewControllerAnimated:NO completion:nil] Is actually a bit of a fiddle. Although you can – legitimately – call this on the presented view controller, all it does is forward the message on to the presenting view controller. If you … Read more