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.
Related
I am designing apple watch application where i need to show top10 feed title and i have successfully shown it. in next step i have to add action event to tap which will redirect user to next screen but i am confused which controller to use here. i have to show all feeds in pagination format and then on click i have to show its detail view.
does anyone tried with this approach? i am using UIButton over there but its having text limitation so cant use it and for tableview it scroll verticaly where as i need horizantle scroll.
alph0x's answer is pretty useful. But you can also do another thing to perform what you are asking in case you only want that the action will do when push in a specific button of the row.
This second solution consists on create a class for the custom row with an IBAction
#import <WatchKit/WatchKit.h>
#interface MyRow : NSObject
// Methods
- (IBAction)buttonClick;
#end
And in the buttonClick method, you can specify the action as in the follow example using pushControllerWithName:context to go to a specific interface controller
#import "MyRow.h"
#implementation MyRow
- (IBAction)buttonClick {
[self goToInterface:#"feedInterface"];
}
- (void)goToInterface:(NSString *)interfaceName{
NSDictionary *contextToSend = [NSDictionary dictionaryWithObjectsAndKeys:#"FeedTitle", #"title",
#"lalala", #"secondValue",
#"lelele", #"thirdValue",
#"other value", #"other", nil];
[self pushControllerWithName:interfaceName context:contextToSend];
}
#end
You can send your row info through context. In that example I have decided to send a dictionary with some values.
In the interfaceName param you have to specify the Interface Controller Identifier that you can set in your storyboard. See the image below:
And tell to the XCode that your table row has the custom class MyRow
Note: don't forget to assign your button to the IBAction method ;)
If I understand, you need to have a table with 10 rows, and you need to be able to tap in one row, and view the details about the one you selected, if thats correct, you only need to use the Method from WKInterfaceTable "- (id)rowControllerAtIndex:(NSInteger)index", with this one (works like UITableView one with the delegate) you can handle every row action after being tapped.
I'm trying to add a view to the main view (a subview) where the user can hand draw, I'm trying to do this using ACEDrawingView, my issue is that I don't know how use the clear, undo etc. features.
This is how I'm using ACEDrawingView which works fine but without any buttons.
1- Created a Simple App project
2- Added a view and set "ACEDrawingView" as its class
3- Complied-ran and it worked great
The question is, how can I clear, undo etc. I tried calling the clear method but nothing happens.
Here is what I have tried that didn't work.
1- Added a button to the main view and created an action for it
2- Imported ACEDrawingView
3- Created an instance of ACEDrawingView
ACEDrawingView *draw = [[ACEDrawingView alloc] init];
4- Called the clear method from ACEDrawingView using the action created in step one
[draw clear];
When I tap the button nothing happens, I don't get any no errors either.
How can I add a clear or undo button?
I'd take a look at the demo application ACEDrawingView provides on GitHub to see how they're doing it. It looks like they use undo, redo and clear in the demo so it should give you a good start.
Here are the steps to add the drawing view and buttons using ACEDrawingView;
1- Create a Simple App project
2- Import (drag and drop) the following four classes to your project
ACEDrawingTools.h
ACEDrawingTools.m
ACEDrawingView.h
ACEDrawingView.h
3- Click the main storyboard and add a view (a view, not a view controller).
4- Select the view you added in step 3 and set "ACEDrawingView" as its class.
5- Compile and run
It this point you should see an empty view where the only thing you can do is draw, no buttons yet.
Now let add some buttons.
6- Go to the main storyboard and add three buttons right below the view you added in step 3, name the buttons clear, undo and redo.
7- Create an action for each button and name them respectably clear, redo and undo
8- Go to viewController.h and add #class ACEDrawingView; right after the import statements and #property (nonatomic, unsafe_unretained) IBOutlet ACEDrawingView *drawingView; right after the interface
9- Go to the main storyboard and link the view you added in step 3 to the outlet drawingView (added in step 8)
10- Got to viewController.m and import ACEDrawingView.h
#import "ACEDrawingView.h"
11- Go to the action for clear and add
[self.drawingView clear];
12- Go to the action for redo and add
[self.drawingView redoLatestStep];
13- Go to the action for undo and add
[self.drawingView undoLatestStep];
14- Done.
You should now be able to clear, undo and redo
Is there any quick way to know which method is called when we do any activity on iOS app. I have the code but this is written by some other team and I am trying to find out the action that is happening when I tap on iPad. Is there any way to know about it. I tried putting break points but it does not help.
Following ways out of many can help you:
Find out all methods that have IBAction mention in the start in your .m file. Put a break point on them.
Goto .storyboard or .XIB file of your respective controller, in the right pane check Connection inspector. This inspector will be divided into two halves first belongs to IBOutlet, you have to check the second one which is IBAction. Find them in your .m file and put a break point on it.
Check .h file for any IBAction methods, find its implementation in .m and put a break point on it.
Trace any IBAction selector(s) specified via addTarget function.
Hope it can help!!
I need help adding an outlet for a text field in XCode 4.6.3. (By way of background, I'm a newbie following Apple's "hello world" tute and am up to here.)
I control-drag the text field inside the method declaration then release. In the pop-up that is displayed, "Connection" defaults to "Action" and is greyed out. I can't change it to "Outlet".
How can I make it editable so I can choose "Outlet"?
Sorry about the lack of images but I'm too junior on Stack Overflow. If you want a visual, feel free to go to the Apple tute, search for the text "you can configure the action connection you just made", then look at the image underneath.
Would appreciate any help as I'm pretty stuck.
D
That happens if you control-drag into the implementation part of the view controller
in the .m file:
#implementation HelloWorldViewController
...
#end
Outlets connect a property of the view controller with an object in the xib file,
and properties are declared in the interface of an object.
Therefore, to create an outlet connection, control-drag into the public interface part
in the .h file:
#interface HelloWorldViewController : UIViewController
...
#end
This is what the tutorial shows.
Alternatively, you can make the property "private" to the implementation by
control-dragging into the private extension interface in the .m file:
#interface HelloWorldViewController ()
...
#end
This works with newer Xcode versions and helps to keep the public interface clean.
Drag and drop textfield to view
click on editor as shown below blue circle
right click on textfield and drag it to viewcontroller.h as shown below
you will seet the box as shown below: enter your name for textfield
click connect.
its done..
hope helps you
I dragged an action to the ViewController.h area (which is my dayNight button), as shown:
//
// ViewController.h
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)dayNight:(id)sender;
- (IBAction)insulationClick:(id)sender;
Now I decide I want it to read:
- (IBAction)dayNightClick:(id)sender;
So I rename it manually as shown in above line. I also rename it in the ViewController.m file from:
- (IBAction)dayNight:(id)sender{}
to:
- (IBAction)dayNightClick:(id)sender{}
When I execute the code I get an error as soon as I use my button.
If I manually rename it (without the word Click) it works again.
Why can't I manually modify the code line. Is there another location where I must rename the code too?
The error is:
Thread1: signal SIGABRT
and points to this line of code in the main.m file:
int main(int argc, char *argv[])
{
#autoreleasepool
{
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
I'm sure this is a really basic question.
This happens because your storyboard / nib still points to the old name. The nib files are essentially just XML files in the background so what you need to do is to open your nib/storyboard go to the inspector that shows all the references. You will find that your old reference to the IBAction still exist there. Delete that by hitting the little cross and drag it into the ViewController again and connect it with the function you renamed it to.
Always remember to update both storyboard + .m if you change your method signature in your header file :)
What I discovered is the following:
Go to the story board (ie the view where you can see your GUI representation of your idevice)
Click on the button you wish to rename (we are going to delete it)
Click the button called "Show the connections inspector" (located near top right hand corner of xcode, and looks like a right pointing arrow in a black circle).
There is a section called "Sent Events", and your button event will have a bubble drawn around it with a link to a second bubble with the name of your IBAction.
Click on the tiny cross (x) in the second bubble to delete the event link.
Now you can start again and rename the button to whatever you like. In my example I renamed it from:
(IBAction)dayNight:(id)sender{}
to:
(IBAction)dayNightClick:(id)sender{}
So this is renaming by deleting all references to the button and simply creating it again. Note the other two places to delete it from are:
In the *.h file, example I deleted this line:
(IBAction)dayNight:(id)sender{}
and in the *.m file, example I deleted this code:
(IBAction)dayNight:(id)sender{}
{
// code in here
}
Yes. The button has an associated target/selector which it calls when it's pressed. Look for a line that contains something like this:
[button addTarget:self action:#selector(dayNight:) forControlEvents:UIControlEventTouchUpInside];
and change the selector here too.