iPhone / iPod Touch SDK | Development Discussion SDK questions. A place for iPhone developers to post code snippets, discuss creating iPhone apps, and any other iPhone developing. |
|
03-27-2008, 11:25 PM
|
|
What's Jailbreak?
|
|
|
|
Tiny question (LaunchMe sample code related)
You know in the "LaunchMe" sample code how you can open Safari & it'll go to apple.com by default? Is there any way to make a UIButton have that function, without all of the messageAlert stuff from LaunchMe? I'm pretty much trying to get a UIButton to open a link to my website in MobileSafari
|
|
03-28-2008, 06:58 AM
|
Green Apple
|
|
Join Date: Dec 2007
Posts: 52
Thanks: 10
Thanked 5 Times in 5 Posts
|
|
sure.
you have to use the addTarget:action:forControlEvents method on the button so the button invokes a method when it is clicked.
in that method you just write
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://yoururl.com"]];
and it should work. I never tried it, I'm just guessing from the LaunchMe-Code, but I guess that should work.
|
|
03-29-2008, 12:24 AM
|
|
What's Jailbreak?
|
|
|
|
Something like this maybe?
Code:
}
- (void)imagePressed:(UIImage *)imagePressed didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != -1) {
// Open Safari only if the user clicked the 'Launch Safari' button, but not if this
// delegate method is called by UIKit to cancel it. In that case, buttonIndex is -1.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:_____________"]];
}
[imagePressed release];
}
It produces no error but, doesn't launch the url when the button is clicked
Here's the code for the button mentioned above
Code:
#pragma mark
#pragma mark White Button
#pragma mark
// create the UIButtons with various background images
// white button:
UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
frame = CGRectMake(xCoord, 300, kStdButtonWidth, kStdButtonHeight);
[self addButtonWithTitle: @"eMail Me"
selector:@selector(action:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];
|
|
03-29-2008, 03:51 AM
|
Green Apple
|
|
Join Date: Dec 2007
Posts: 52
Thanks: 10
Thanked 5 Times in 5 Posts
|
|
Quote:
Originally Posted by celebi23
Something like this maybe?
Code:
}
- (void)imagePressed:(UIImage *)imagePressed didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != -1) {
// Open Safari only if the user clicked the 'Launch Safari' button, but not if this
// delegate method is called by UIKit to cancel it. In that case, buttonIndex is -1.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:_____________"]];
}
[imagePressed release];
}
It produces no error but, doesn't launch the url when the button is clicked
Here's the code for the button mentioned above
Code:
#pragma mark
#pragma mark White Button
#pragma mark
// create the UIButtons with various background images
// white button:
UIImage *buttonBackground = [UIImage imageNamed:@"whiteButton.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:@"blueButton.png"];
frame = CGRectMake(xCoord, 300, kStdButtonWidth, kStdButtonHeight);
[self addButtonWithTitle: @"eMail Me"
selector:@selector(action:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];
|
well, yeah, that can't really work. I would be surprised if that doesn't produce any errors.
first of all: there is no such thing as a "imagePressed"-method. just because there is a method for UIAlertView-Buttons for that that doesn't mean a method like that exists for everything. there is no imagePressed-method and therefore it is never invoked.
secondly: there is no addButtonWithTitle: method. I mean there is one, but only to add buttons to a UIViewAlert, not to add buttons to anything else. so there is no such thing.
I would heavily advise you to just search the iPhone OS documentation for those things next time, because you find really all the infos you need there (help->xcode documentation or something like that in xcode).
what you have to do: create a UIButton object (for that look for UIButton in the iPhone OS Docs) and then use addTarget:action:forControlEvents on that button (look for that method in the docs).
you can look at the "Hello World" example from apple to see how buttons work and how you notice a click on a button.
regards
|
|
The Following User Says Thank You to BlackWolf For This Useful Post:
|
|
04-03-2008, 07:00 PM
|
|
What's Jailbreak?
|
|
|
|
Quote:
Originally Posted by BlackWolf
well, yeah, that can't really work. I would be surprised if that doesn't produce any errors.
first of all: there is no such thing as a "imagePressed"-method. just because there is a method for UIAlertView-Buttons for that that doesn't mean a method like that exists for everything. there is no imagePressed-method and therefore it is never invoked.
secondly: there is no addButtonWithTitle: method. I mean there is one, but only to add buttons to a UIViewAlert, not to add buttons to anything else. so there is no such thing.
I would heavily advise you to just search the iPhone OS documentation for those things next time, because you find really all the infos you need there (help->xcode documentation or something like that in xcode).
what you have to do: create a UIButton object (for that look for UIButton in the iPhone OS Docs) and then use addTarget:action:forControlEvents on that button (look for that method in the docs).
you can look at the "Hello World" example from apple to see how buttons work and how you notice a click on a button.
regards
|
THANK YOU!!!!!!! I finally got the email button to work 1 major feature down, 1 more major one left to got & then I'm all set for version 1.0 of my app
I do have 1 other question though, I'm using the HelloWorldClassi app as a base & I'm trying to get whatever text I enter to be entered into Google
Code:
//This method is invoked when the Hello button or return key is touched
- (void)hello:(id)sender
{
[self.textField resignFirstResponder];
NSString *nameString = self.textField.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/search?hl=en&q=",nameString]];
}
But, I keep on getting this one error
Code:
error: too many arguments to function 'URLWithString:'
Any ideas?
|
Last edited by celebi23 : 04-03-2008 at 07:32 PM.
|
04-04-2008, 11:28 AM
|
|
Moderator
|
|
|
|
Quote:
Originally Posted by celebi23
THANK YOU!!!!!!! I finally got the email button to work 1 major feature down, 1 more major one left to got & then I'm all set for version 1.0 of my app
I do have 1 other question though, I'm using the HelloWorldClassi app as a base & I'm trying to get whatever text I enter to be entered into Google
Code:
//This method is invoked when the Hello button or return key is touched
- (void)hello:(id)sender
{
[self.textField resignFirstResponder];
NSString *nameString = self.textField.text;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/search?hl=en&q=",nameString]];
}
But, I keep on getting this one error
Code:
error: too many arguments to function 'URLWithString:'
Any ideas?
|
Indeed there are too many arguments... notice your "," before nameString. I would suggest you build your complete URL variable and then use that in your "URLWithString" statement.
Something like.... (mind you I have no idea if it works...)
Code:
NSString *nameString = [[NSString string] stringByAppendingString: @"http://www.google.com/search?hl=en&q="
stringByAppendingString: self.textField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:nameString]];
Here's how my navbar buttons call my help page and my donate page:
Code:
case 1: //
switch( aButton )
{
case 0: {
NSURL *donateURL = [NSURL URLWithString:DONATE_URL_STRING];
[self openURL:donateURL];
[self terminate];
break; //Donate
}
case 1: {
NSString *helpLink = [NSURL URLWithString:INFO_URL_STRING];
[self openURL:helpLink];
[self terminate];
break; //FAQ
}
}
break;
}
Where both "*_URL_STRING"'s are constants set in the header file.
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
Copyright © 2007-08 by ModMy, LLC. All rights reserved. You may not copy anything on this site unless you link to the original.
All times are GMT -6. The time now is 10:15 PM.
Powered by vBulletin® Version 3.6.10 Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5
ModMyiFone.com is an independent
publication and has not been authorized, sponsored, or otherwise approved by Apple, Inc or Cisco Systems, Inc. The information contained on this site is for educational purposes only.
Forum skin by poetic_folly
| |
|
|