How to add existing code to a story board skeleton UI? - swift

I have made a tabbed application using the storyboard, and each tab has its own view controller. Currently, each tab does nothing but display an image, but I need to add some existing code to each view controller that some partners have coded up. I'm very new to xcode and swift, and am wondering how to do this. These were coded without using the storyboard, but mine was exclusively using the story board. Would I just need to copy the viewcontroller.swift onto each one that I have in my skeleton UI?
Thank you for any help.

Would I just need to copy the viewcontroller.swift onto each one that I have in my skeleton UI?
Not just copy it. There are two stages. To illustrate, let’s say that we’re going to have the code for one of your tabs in a view controller called MyViewController1. Then:
You need to declare class MyViewController1. This is usually done in a file MyViewController1.swift (though no law requires that).
You need to tell the storyboard that this view controller is a MyViewController1 (and not merely a plain vanilla UIViewController). You do that in the scene’s view controller’s Identity inspector.

Related

Custom, swipe up, navigation on every page xcode

New to xcode so I am not quite understanding views and view controllers.
I want a simple 3-4 page app with swipe up navigation, similar to this question:
How to mimic iOS 10 maps bottom sheet
How would I implement this on every page instead of hard coding into each one?
If I decided to add more pages that would become tedious and inefficient.
I have used Ionic 2 and you simply make an outlet for all your views to go into, with your navigation surrounding it.
I saw containers in xcode, but that seems to be more difficult then a push/pop navigation.
What is the best way to implement this in the storyboard?
Yes, I have no code, but the story board does not need code
How would I implement this on every page instead of hard coding into each one?
If you know how to do it, you should never need to copy it to each page. You could make a base class or (even better) a protocol with extension that implements it.
You could also make a swipe-up view that puts gestures into its parent view when it's added.
If you do that, you could add the storyboard directly (add a UIView, change its class to yours)

iPhone App Development - Few beginner questions

I've been tasked with creating an app, but I have zero experience with iOS development. I have general programming knowledge, particularly with Java, JavaScript and PHP (I'm more a web developer than a programmer). I have dabbled with C, Xcode and various other languages and IDEs in the past, but I remember very little.
I've been following Apple's Developer Library tutorials, and I'm at around the Language stage, where I'm come to a grinding halt. While I slowly progress through learning the basics of Objective-C, there are a few things I'm very confused about regarding development in Xcode that various tutorials seem to completely skip over or just imply that you know what to do, or some just stop right before the part I'm having trouble with.
1) Storyboard - yes or no?
Is it better to start with an empty application and work with the
files or create a template (in my case a tabbed application) and work
with the storyboard?
2) If using a storyboard, do I still need to have a .xib?
Are the User Interfaces more like global templates that the view controllers implement?
If I wanted a different layout for each tab of my app, would I create a .xib for each tab, or just edit the controllers in the storyboard? Am I correct in understanding that the storyboard can have multiple instances/relationships of the same controller, in which case having .xib's would make more sense?
3) If using storyboard, where do the implementation and source files come from?
This is probably a stupid question. I know you can just add them via File -> New, but I don't know how to associate those files with a view controller. Is there a way to have the files created automatically when adding a controller into the storyboard?
Since you're just starting, you should use Storyboards because it lets you link different view controllers(pages on your app, essentially) visually and outside of code. For example, you can link your UITabbedViewController (the part that manages the content of the other tabs) to the pages that represent the content of the different tabs. Basically, your storyboard would have the tabbed view controller in a parent-child relationship with the sub-controllers. You would have one instance of each -- the tabbed view controller, managing an instance of each of the tabs' content and controller. This is the same regardless of Storyboard or xib, but you can connect this more easily in the storyboard.
You can still use a .xib(nib) file for stuff like Custom Table cells or in cases where you want to separate a view element or controller from a storyboard where there are other constraints.
In the storyboard, you subclass the controller class on the sidebar in the visual editor by entering your subclass of say UITabbedViewController in 'Custom Class'. In your file associated with 'MyTabbedController', you implement your stuff.
Great book:
http://www.barnesandnoble.com/w/beginning-ios-6-development-david-mark/1113216077?ean=9781430245124
Good luck!
Storyboards can be appropriate for small applications, where you have ten or twenty screens. When your app contains more than that, you will just get lost in storyboard schema, where all your view controllers will visually look the same.
I prefer not to use storyboard, just separate xib files for each controller.
If you use storyboard, you can create xib files for other parts of application that is not related to SB, and view controllers that is involved in SB has their interface stored in SB, meaning you will have to design them in there, in this one huge storyboard file. I find this very uncomfortable.
As you are new to IB, I would like recommend you to take a look at Auto layout. There is no magic anymore :)
To answer on a point-by-point basis:
I typically use the "Single View" template. It provides everything you need for your first view and can take it from there. It's a clean slate but it already has that first view which will be exactly the same code in 99% of the applications you make.
No, the storyboard file is your xib. You used to have to make a new xib for each new layout, but then Apple introduced storyboards. A storyboard is basically all of your xib's in one file. Rather than make a new xib, drag a new ViewController object onto the document. You typically only have 1 storyboard file or 2 if you want to support both iPhone and iPad layouts.
I don't think you can have it create your source files automatically, but its fairly easy to connect them manually.
Select the ViewController that you want to connect to your source files by clicking on that black bar beneath it. Then go to the bar on the side and go to this panel:
There you enter the name of your custom ViewController subclass where I have put "MyViewController". Hope that helps!

How to swipe out of table view controller (not the cells) - iPhone

I have a tab bar app. It has 5 tabs. I have the 3rd tab connected to a navigation controller that's connected to a table view controller. Simple enough, right?
Well, I recently decided to implement left/right swipe gestures as an optional way to navigate left and right to each tab in the tab bar controlled app. The gestures work great, except I don't have a way to implement the gestures on the 3rd tab (the one with the UITableView) because there's no implementation file associated with it.
And that's my question: How can I implement the gestures on the third tab so I can navigate AWAY from it by swiping?? I can swipe into that tab, but not out of it. I'm fairly new to iOS dev, and I really appreciate any help!
(Using ARC, Storyboard, & Xcode 4.5.2)
EDIT:
Here's a screenshot of how my Table View Controller is connected to my Tab Bar Controller:
SOLUTION:
The answer marked correct is indeed correct. It explained to me how to make a subclass of my UITableViewController. But I was also wondering how to implement my TableView programmatically... so after a bit of searching, this link pretty much shows exactly how to do that: Ruchi Raval's post on mobisoftinfotech. However, I ended up just following the steps in the link, and I ended up deleting my original UITableViewController because I had created a new one through the steps in the link and connected it to my NavigationController through the Storyboard (ctrl + drag).
Hope this helps anyone that had my same quesions!
To control gestures on a VC, you'll need your own subclass. Create a new objective-c object named like MyTableViewController. Xcode lets you specify a superclass when creating the files, use UITableViewController.
Xcode provides some template code in the new files -- leave that alone to begin with. In storyboard, I think the tab you're trying to fix has a navigation (vc) whose root vc is a UITableViewController.
Select that table vc and select the Identity inspector (upper right, third tab from the left). You should see a section called custom class. The current class is probably UITableViewController. Change that to MyTableViewController, or whatever you named the new class.
Build and run. Now you control that table vc. Back in it's implementation file, you can add your gesture code in viewDidLoad:.

How do I access the view that is one level up from my current view in a navigation-based iOS app?

I'm diving into iOS development and am getting familiar with navigation view controllers. I'm trying to build a simple app with a table view that allows me to add objects to it. So far, I have a table view with an add "+" button in the nav bar that allows me to load my CreateObjectView and display it modally so the user can define the new object, but when they click the save button, I don't know how to send that object data back to the parent view that created the CreateObjectView that contains the object data.
When I create the child view (CreateObjectView), I could pass into it a pointer to the current view before I push it onto the nav stack, but this just feels dirty and circular.
I was reading about protocols and delegates and I could use that approach as well, but it also feels circular.
This question seems like it would be a common design issue to any tableview-based or nav-based app, how should I probably access the view that is one level up from my current view in a nav-based iOS app?
Thanks so much in advance for all your help!
It feels circular at first, but it is the right way. Before the UINavigationController pushes the new view controller you should set the delegate to the current view controller - assuming that's the object you wish to communicate with. Of course you could set it somewhere else instead, such as to a central Core Data handler. Then when you need to communicate call the delegate with the method you have defined. I've done several navigation apps like this after seeing Apple's cookbook example, and it really works well.

Can I add more Content Views if I had selected an View-based Application Template?

I started programming with an View-based Application Template. Now, I figured out that I will need more Content Views (I.e. settings pages, statistics page). I don't really want to make a new project in XCode and struggle around with porting my nib file to that new one since it is pretty complex. I would just want to add that Content Views. Or would that be more work than just making a new project and porting everything into it?
It should be pretty easy to add more views to your project. Usually one creates a new nib file containing the new view, and a UIViewController (or a subclass thereof) to go alongside with it.
For modal views you can then use the presentModalViewController:animated: method of your current view-controller to show the new view.
Or use a UITabBar to let the user switch between diffrent views.