I inherited a swift project that I need to change: I need to add a line in the list of settings, with a segue to a new ViewController.
Is this the correct order of actions:
first build the new ViewController (can I copy-paste from another ViewController?)
then add a new line in the "settings" list ( can I copy-paste another listItem?)
then add a segue from the new listItem to the new ViewController ( any hint on how to do it is welcomed...)
or
can I copy-paste a listItem and it will also copy its segue and its activity?
what is the correct way?
( I did the same change in android project, changing android.activity.xml is much more easier than using xcode.storyboard.userInterace )
I would recommend to use separate .xib for the new ViewController (IMO it will be much easier and harder to mess things up)
actions:
add new line in the settings (with it's own action). Without any code it's hard to say if you can copy any of the previous but I would assume you can (if it's in the storyboard just check the outlets of the copied and remove them)
Create new ViewController with .xib file (File -> New -> File -> Cocoa Touch Class (subclass of: UIViewController, make sure also create XIB file is checked)
In the action of the item (assuming the previous ViewController is embedded in NavigationController)
let myNewFancyViewController = FancyViewController()
show(myNewFancyViewController, sender: self)
Modify the new view controller
EDIT:
Since you added image of the storyboard:
Settings looks like to use static table view cells, it should be easy to copy one of them and add it where needed. Copying ViewController is also possible just make sure that all outlets (views/actions) are removed. Last step is just to option + drag from the cell that you created and select show (You can add identifier to the segue if you want to pass data between the view controllers)
NOTE: after copying the new view controller seems to remove all the existing outlets, so it shouldn't be a problem
I know the second button here shows the Assistant editor:
And I know how to make an Outlet and Action by Control dragging from an object in the Interface Builder to the View Controller code. Ideally it should be as easy as the documentation makes it look:
But this is usually what Xcode actually looks like for me when I press the "Assistant" editor:
A mess. I have to minimize lots of things, try to get the storyboard object in view, and then go find the right View Controller. All this before I can do the Control-drag.
Is there a way to make the connection without using the Assistant editor? (And preferably without having to type a lot of code in myself.)
Don't press the assistant editor button. Sometimes it opens a random file instead of the one you want.
When you are in Storyboard, Option click on the .h file that you want to open in the Project Navigator. This will open the proper .h file to add the outlets or actions.
When you're done, close the Assistant editor right pane (which is displaying the .h file) and you will be back in Storyboard.
Yes, you can do it without the Assistant editor and without writing lots of code. It requires learning to do two things:
Use code snippets
Use the Connections inspector
Create code snippets for the IBOutlet and IBAction
Normally when you create an IBOutlet and IBAction with the Assistant editor it automatically adds code like this to your View Controller:
#IBOutlet weak var myOutletName: UIButton!
#IBAction func myActionName(sender: AnyObject) {
}
You could type this all yourself and then add the connection in the Connection inspector, but why do all that typing? Just make a code snippet that will autocomplete. Drag the code to the Code Snippets library in the Utility panel.
Give it a title and most importantly, a Completion Shortcut. I called mine "ibaction" for the #IBAction code.
Now all I have to do is start typing "ibaction" and I can autocomplete the code snippet. I only have to edit the name of the action. It is a similar process for the Outlet.
Read more about creating code snippets:
Xcode Snippets
Creating a Custom Code Snippet
Now all you have to do is connect the IB object to the code.
Make the connection with the Connections inspector
First, click the object in the storyboard that you want to connect. For me, I am using a Button.
Then select the Connections inspector. It is on the far right.
Alternatively, you can right click or control click the object to get a menu.
Then click the New Referencing Outlet to connect it to your Outlet or the Touch Up Inside under Sent Events to connect it to your Action.
For whatever reason I find that sometimes I need to drag just a little bit after clicking the "+" button to get the menu of available connections to show up.
This is my first attempt at creating an app. and using Xcode.
The reason this question hasn't been answered before is because there are no answers to my question from the perspective I'm currently at, namely, I've followed the instructions on this URL to creating an app:
http://www.wikihow.com/Make-an-iPhone-App
I have got to the Part 3 of 5: Creating the App, section 6 - thereafter my question is not answered - which is how to make my button make a call when tapped.
Therefore I am now, in Xcode at the point where (remember I followed those instructions on the linked page) I have my one button on the screen but ALL the instructions I could find doesn't address exactly what I need to do to make that button make a call.
Some examples show code like here: Making a Button Call a Phone Number in iOS
but doesn't tell me what to do with that code, I'm new to all of this so finding out the exact steps from this point has been brutal at best. Also, all the code I have tried pasting into sheets that have code in them (by clicking around) the code shows errors - all the code I've obtained from the web.
Any help?
P.S. On this page, a poster says that there is actually a button that is associated with making calls, but I again, know not where to find this…
http://www.insanelymac.com/forum/topic/126918-initiating-a-call-on-iphone/
Open your xib/storyboard side-by-side with your view controller implementation, hold Control and drag your button (interface builder) to your implementation. Xcode should generate an IBAction for you automatically.
On your code, call:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:911"]];
Basically you need to add the code you found into a 'method' in your appdelegate.m file. This 'method' is a segment of code that is executed whenever it is told to.
So in appdelegate.m, add in the lines above #end
- (IBAction)makeCall:(id)sender {
}
Now paste the line of code you found in between the two curly brackets. Now whenever 'makeCall' is called that line of code you found will be executed which will make a phone call (hopefully)
The next step is making the button tell the 'makeCall' method to run. In order to do this you need to 'declare' the makeCall method, it's the equivalent of putting an item up on sale on eBay: in the previous step you made the item(method), now you want to show the world it's available.
Appdelegate.h is the equivalent to eBay/Craigslist/gumtree in this scenario: add this line of code anywhere above #end:
-(IBAction)makeCall:(id)sender;
Now the final step is to link your button to this, and it's the easiest part. Go back to your interface builder and click on your button. Right click the button and drag a line to the blue box on the left called 'AppDelegate' (this is the files you added code to earlier, remember?) and select makeCall from the little list that pops up. You have successfully linked your button to your method, so now when you click the button you should be able to make a call!
If you want to know more about the specifics of the code you just added, IBAction is the type of method, and it means a method that can have buttons linked to it in interface builder. The (id)sender part means that whenever the method is called, the object/button that called the method is passed along so the method can see who 'sent' for it.
Edit: Ok since you're using storyboards we'll need to create what's called a 'view controller'. This basically delegates and controls (hence the name) whatever is on your phone's screen.
So create a new class by going to file -> new -> cocoa class, and in the fields call it ViewController and make it a subclass of UIViewController.
Now we'll need to copy all the code that we added to appdelegate.h and appdelegate.m over to viewcontroller.h and viewcontroller.m, with the code we added to the appdelegate.h being copied to the same place in viewcontroller.h etc.
They should look something like this:
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
-(IBAction)makeCall:(id)sender;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)makeCall:(id)sender {
}
#end
With makeCall having the line of code that you pasted in it earlier.
Now go back to interface builder and click on the little yellow square inside a cube above your screen, and then click on the newspaper-looking icon on the right hand side:
In the class field type in ViewController (it should automatically complete it for you) and then go to the arrow icon tab (which is known as bindings):
Click and drag the little circle beside make call onto your button, and select one of the 'touch down' options that appear. These just mean when to call that method i.e as soon as the user presses the button or when they lift their finger off it. They should now be linked. Give it a try and let me know how it works.
I have a scroll view that loads a series of XIBS as pages. One of the XIBs (iPhoneFirstPage) is a UIView that has a twitter button that I would like to link to another ViewController in the XIB. The only problem is when I create a subclass of UIView to add an IBAction to the button, the subclass doesn't show up under the "Custom Class" drop down on the UIView. How would I do this? Is there a better way?
Save both files (Cmd + S) and try again.
Cmd+Shift+K to clean and try again.
Just type the name in--it should build fine assuming you've typed the right name and it can find the file.
Restart Xcode and try again.
Note: This is a list of 4 different things to try. Any single one of them should correct the problem individually--you shouldn't necessarily need to do all 4 things.
I have a UIViewController I created in my apps storyboard, as well as a custom UIViewController subclass which I added to the project (and is correctly in the compile phase for my target). However when I go to set the 'Custom Class' property on the view-controller in Storyboard my custom class does not show up on the list.
Checked that the class is part of my app's target, not tests'
Double checked that it is correctly a subclass of UIViewController
Compiled program just to make sure xcode was working with latest information
Restarted xcode
What would cause my class to not show up in the 'Custom Class' drop down?
Two ways I found that solve the problem but they are work arounds:-
Just type the view controllers name in the text field, or
close the project and then reopen it and in the project initialization it places the file on the list.
If you still have your problem or for those who could have the same problem:
Make sure to select on your storyboard your "ViewController" instead of your "View" (which is automatically selected when you click on the view in the storyboard). The difference between those two is that when the view controller is selected, a blue rectangle pop up around your app. To be sure to select the view controller, open the document outline and select it directly in your storyboard hierarchy.
I would try the following:
Check that the file implementing the class is part of the build phases (check under target > build phases)
Add the .m file to build phases (if it isn't already).
Restart Xcode.
You can fix this by editing the XML of your Storyboard.
Right-click your My.storyboard entry in the Project Navigator panel and select the Open As->SourceCode menu choice. Find your view controller entry in the XML, and add the attribute customClass="MyController".
Save the storyboard.
Right-click your My.storyboard entry in the Project Navigator panel again, and select the Open As->Interface Builder - Storyboard menu choice.
The custom class entry will now contain your MyController class name.
Make sure your class inherits from UIViewController.
#interface ClassName : UIViewController
In Xcode 8, a few of my classes had the wrong path (case sensitive) specified for their file locations.
MyProject/mysubdirectory/MyViewController.xib (.m, .h)
vs:
MyProject/MySubdirectory/MyViewController.xib (.m, .h)
Really not sure how it ended up in that state, but my project exhibited the exact same behavior as above (no outlets/actions displaying in IB), and fixing that path fixed the problem.
I fixed this two different ways. One way was by I opened the .pbxproj file and fixing the case sensitive issue manually. The other way that worked was by tapping the folder icon under the Identity and Type section of the File Inspector tab of the file, and re-selecting the file there.
Click on a different view controller in the storyboard, then click on it's custom class pulldown to confirm the new class is listed, but don't select it. Click back on the new view controller you made and you should see it now listed in its custom class pulldown menu. odd, eh? just forces a refresh I think.
I had been having the same issues as described in this problem. However, none of the suggested answers fixed it for me. My project compiled OK without warnings or errors, but, in the .h file there were no 'outlet' indicators to indicate that my outlets had been linked to storyboard elements.
Additionally, attempts to create new outlets in my code, by right-click and dragging into my header file, were not recognising my header source as a potential target for this operation. And furthermore, my Class did not make an appearance in the Custom-Class dropdown for the ViewController's property inspector panel.
And yet, the project compiled OK.
Closer examination showed that I had defined my own class in the following manner...
#interface KJBMainDataViewTrackConMk2<UIScrollViewDelegate> : UIViewController
which apparently compiles nicely.
But, if this is changed to the following, (moving the protocols to the end)...
#interface KJBMainDataViewTrackConMk2 : UIViewController<UIScrollViewDelegate>
Then everything springs to life. All outlets are suddenly indicated as being 'connected' with a storyboard element. And right-click dragging starts to work again, and my custom class appears in the custom-class drop-down in the property inspector panel for the storyboard ViewController!
Other answers here probably represent the most likely causes of this condition, but, I felt it worth mentioning at least this one other potential cause.
I had the same problem, but none of the other solutions worked for me. The issue for me was that I had a Mac and iOS target, both with their own versions of the same view controller. For example, I had a .h/.m pair of files named FooViewController for Mac and another .h/.m pair of files named FooViewController for iOS. Each pair was properly included with their respective targets, but for some reason Xcode does not like it and my view controller would not show up in the Custom Class dropdown in the view controller in the storyboard. I ended up renaming my class in the iOS view controller and it immediately showed up in the dropdown.
In my case, I drag a new TableViewController object to the storyboard, but I add a new file which's subclass is "UIViewController".... Then, I add a file which's subclass is "UITableViewController", problem solved!!
For those of you who are still having this problem after trying all the way around is probably because you clicked the View instead of ViewController.
You have to choose the file when ViewController is clicked.
This solved my problem.
I happened to come across this problem, and had no luck after trying the previous suggestions. Finally I found the reason is the header file's type is set to C++ header. After changing it to C header (like all the other header files), the class appears in the drop list. Don't know why though... Possibly helpful for others so I post it here.
Storyboard is looking for the custom class but physically its no there and its not displaying the custom class name in the list and also not displaying the outlets . Following solution perfectly worked for me.
Just copy your code some where else.(Lets say on desktop)
Open your existing code.
Delete the custom class file.(Move to trash)
Now add files from copied project folder (From desktop)
Don't forget to check "Copy if needed" check box
Open the story board and bingo you will get your custom class files in dropdown
be sure initially not adding the CustomViewController to any group or folder. place it in the root of your app.
none of the above(or below :) helped me. though I found that
after adding new viewcontroller to storyboard (just by dragging it in)
and adding my class by File\New\File\Objective-C Class, give it a name, no XIB, Next, Create
if I select my viewcontroller in storyboard and try to assign my class to it - my class is not there
BUT
if I click on a view that is in the viewcontroller itself then click on a class dropdown menu in Custom Class
AND THEN
select viewcontroller (click on a bar below the viewcontroller) and now again click on a class dropdown in Custom Class my newly added class magically shows up.
weird, must be a bug with refreshing. Xcode Version 5.1 (5B130a)
Make sure you select View Controller to which you want to attach your class. The easy way is open Document Outline and choose your view controller (When you tap on it form the Storyboard sometimes it simply doesn't choose the object you meant).
for my case, somehow bundle resources got deleted, so I added back and worked!
Build Phases -> Add Build Phase -> Copy Bundle Resources
find your file
Then drag and drop your file there
Then make sure your target membership is checked.
In my case, I selected the wrong UI.. so I deleted the class file and created a new one and selected the correct parent class
I had to restart XCode 7.3 (7D175)
What worked for me was, click on the file in the Project Navigator, then, in the File Inspector under "Identity and Type" beneath the "Location" dropdown box, click on the little folder icon and then select the file in the popup window.
Try to clean your project, and also restart your Mac. One peculiar thing that I did was change all the Custom Classes names and build the project again. It worked!
For macOS projects, creating a new class generated a class inheriting from NSObject instead on NSViewController.
Change from
import Cocoa
class AppsViewController: NSObject {
}
to
import Cocoa
class AppsViewController: NSViewController {
}
I was fairly frustrated with this issue as none of the answers above had solved my problem.
In my case: I was in the middle of working on swift view controller file and was making active changes (such as creating a custom collection cell class). I had not finished the code block and left it open like so :
class tableViewCell: UITableViewCell {
}
class collectionCell:
class viewController: UIViewController {
override func viewDidLoad(){
super.viewDidload()
}
}
Note the incomplete code block 'collectionCell2'
This was enough for xcode to not recognize my viewController file as such.
Once I completed this block the file reappeared in my xcode as an option.
Very silly and simple.
Make sure the view controller is matching with the same Type in the storyboard .
In my case swift file name was different then swift class name i.e
file name was ViewControllerTest.swift
and class name was ViewController.swift
after changing both to common name solved my problem
Restart Xcode after above changes
Make sure the class name of the ViewController is the name that you want. i.e.
class MyCustomNameViewController: UIViewController {
.
.
}
Changing just the filename is not enough.