1

I am creating my application without using StoryBoard, basically creating Widgets (UIViews) and adding them to my rootController.view

self.view.addSubview(view)

My question is fairly basic, but I would like to have some guidance in it. How do I bring up a new activity (as in Android) programmatically and have its rootViewController set to an object of a different UIViewController.

  • Is the concept of a window the same as an activity (in Android)?
  • How can I make the new window/activity slide in from the right, say when a button is clicked?

p.s I am building this application in Swift.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Prakash Raman
  • 13,319
  • 27
  • 82
  • 132

2 Answers2

1

So, I haven't done much Android development, only some small projects. I have done quite a bit of iOS development though. While the concepts are similar, they are not exactly the same.

The piece of code you posted where you add a subview to your existing superview (current view controller) will not get you the effect you're looking for. What you want to do is push your next view controller on to the screen.

self.navigationController?.pushViewController(yourNewVC, animated: true)

This will give the effect of sliding the view controller being pushed in from the right as you described.

amaceing
  • 56
  • 3
  • Ah interesting. So what you are saying is that my "window"'s rootViewControlller should a navigationController ? – Prakash Raman Jul 07 '15 at 15:00
  • 1
    Yes, I think so. I don't know if that's the only way, but it's the only way I've ever done it. I don't ever use storyboards but I do use .xibs and Interface Builder for almost everything. In my AppDelegate.swift file I always set my rootViewController as a UINavigationController and this allows me to push and pop view controllers throughout my apps as I please. – amaceing Jul 07 '15 at 15:01
  • Right thanks. So I guess we should have only 1 navigation controller throughout the app ? Also. Where should i create the buttons. In the app delegate itself ? In your example which class does - self - point to ? – Prakash Raman Jul 07 '15 at 15:45
  • 1
    Sorry I'm at work right now and couldn't respond sooner. So, the buttons can be created programmatically, but not in AppDelegate.swift. Buttons should be created in the View Controllers in which they will be used. In that line of code, self refers to the View Controller I'm currently writing the code in. Is this your first iOS app? If so, I really recommend buying BigNerdRanch's iOS guide. It's in ObjC but most of the stuff you can translate to swift with some googling. It's also not a bad idea to learn ObjC straight up as a lot of code is still in ObjC. – amaceing Jul 07 '15 at 21:07
  • Not a problem :) Thanks! Right, I was wondering if self was the ViewController or the AppDelegate. Makes sense now, thanks! Yes, this is my first APP, its really exciting! Shall look at the book now :) – Prakash Raman Jul 08 '15 at 19:27
1

Window is not activity.

ViewControllers are activity.

You can push or present new view controller to go to new activity (viewcontroller) You can see here how can you push or present view controller.

Community
  • 1
  • 1
NSSwift
  • 215
  • 2
  • 8
  • Ah, thanks. That cleared that :) From what I understand, I can push a ViewController to my NavigationController. Although, what if I do not have a NavigationController. How do I bring up a new ViewController ? – Prakash Raman Jul 08 '15 at 19:29
  • 1
    @PrakashRaman If you don't have NavigationController then you have only one option to present view controller, you can not push view controllers (push is stack of view controllers which is maintained by UINavigationController) – NSSwift Jul 09 '15 at 04:50