Ios 如何通过按钮打开URL?
我有一个Ios 如何通过按钮打开URL?,ios,iphone,objective-c,ios7,Ios,Iphone,Objective C,Ios7,我有一个UITextView,在那里我输入了一些文本。我有一个ui按钮。我想单击按钮并打开我在UITextView中键入的文本应该出现的URL。我正在为IOS 7编写一个字典应用程序,当我想让人们翻译我的文本时,需要这个功能。我知道如何只打开URL,但我的文本应该显示在URL中的框中。有人能帮我编码吗 SozdikViewController.h @interface SozdikViewController : UIViewController @property (strong, nonat
UITextView
,在那里我输入了一些文本。我有一个ui按钮
。我想单击按钮并打开我在UITextView
中键入的文本应该出现的URL。我正在为IOS 7编写一个字典应用程序,当我想让人们翻译我的文本时,需要这个功能。我知道如何只打开URL,但我的文本应该显示在URL中的框中。有人能帮我编码吗
SozdikViewController.h
@interface SozdikViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextView *textView1;
@property (weak, nonatomic) IBOutlet UIButton *Button_Ask;
-(IBAction)ask;
@end
这是我的SozdikViewController.m文件
#import "SozdikViewController.h"
@interface SozdikViewController ()
@end
@implementation SozdikViewController
-(IBAction)ask
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.audaru.kz/?product=SoylemMT&word="]];
}
...
@end
如果我理解正确,您只需要翻译在UITextField中键入的文本。所以你必须简单使用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://www.audaru.kz/?product=SoylemMT&word=%@", textView1.text]]];
并将%@
移动到url中的正确位置。
希望有帮助像这样试试
//Append your word in the text field to the url
NSString *urlString = [NSString stringWithFormat:@"http://www.audaru.kz/?product=SoylemMT&word=%@",textField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
您可以通过以下方式从文本视图获取URL:
NSString *urlString = self.textView.text;
然后,您可以通过以下方式打开url:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
我在代码中加入了注释来解释每个步骤,但是您应该能够使用
stringWithFormat:
构造一个NSString*
,并将您的URL(http://www.audaru.kz/?product=SoylemMT&word=%@
)和[[self textview 1]text]
然后,您只需将其传递给NSURL*
的实例,并使用[[UIApplication sharedApplication]canOpenURL:][/code>检查是否可以打开URL。检查完后,如果为真,则可以使用[[UIApplication sharedApplication]openURL:][/code>打开它。还要确保所有的IBOutlets
链接正确,否则它将无法执行任何操作
SozdikViewController.h
// Start of interface file
@interface SozdikViewController : UIViewController
// Property declarations
// Note the lower cases at the beginning of the property variable names
@property (strong, nonatomic) IBOutlet UITextField *textView1;
@property (weak, nonatomic) IBOutlet UIButton *buttonAsk;
// Method declarations
- (IBAction)ask;
@end
// end of interface file
SozdikViewController.m
// Start of implementation file
#import "SozdikViewController.h"
// You are clearly not using the class extention so why is it there
// Removed it as it is pointless being there if you aren't using it
@implementation SozdikViewController
// Synthesize is done for you automatically
- (IBAction)ask
{
// We are going to do it like everyone says but add a few checks in
// First lets check to make sure that the textView1s text isn't nil or an empty string
if([[self textView1] text] != nil && ![[[self textView1] text] isEqualToString:@""]) {
// Construct the URL with the word that has been passed in
NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.audaru.kz/?product=SoylemMT&word=%@", [[self textView1] text]]];
// Next check to make sure that the URL can actually be opened
// otherwise forget about it as it would be pointless.
if([[UIApplication sharedApplication] canOpenURL:urlString]) {
// We can open it so lets open it
[[UIApplication sharedApplication] openURL:urlString];
} else {
// else do something to tell the use it can't be done.
}
}
}
@end
// end of implementation file
NSString*url=self.textView1.text;我强烈建议不要把所有这些都放在一条线上。您应该首先获取文本,将其转换为URL,然后打开它。如果按原样编写代码,那么代码很难阅读。@LordZsolt我试着把代码写成作者的问题。是的,这是真的。虽然如果他的编码风格有点怪异,你不应该建议他继续使用这种编码风格,但当PM/老板告诉他要改变时,他很难改变。
// Start of implementation file
#import "SozdikViewController.h"
// You are clearly not using the class extention so why is it there
// Removed it as it is pointless being there if you aren't using it
@implementation SozdikViewController
// Synthesize is done for you automatically
- (IBAction)ask
{
// We are going to do it like everyone says but add a few checks in
// First lets check to make sure that the textView1s text isn't nil or an empty string
if([[self textView1] text] != nil && ![[[self textView1] text] isEqualToString:@""]) {
// Construct the URL with the word that has been passed in
NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.audaru.kz/?product=SoylemMT&word=%@", [[self textView1] text]]];
// Next check to make sure that the URL can actually be opened
// otherwise forget about it as it would be pointless.
if([[UIApplication sharedApplication] canOpenURL:urlString]) {
// We can open it so lets open it
[[UIApplication sharedApplication] openURL:urlString];
} else {
// else do something to tell the use it can't be done.
}
}
}
@end
// end of implementation file