我有一个UITextView
我输入了一些文字。我有UIButton
。我想点击按钮并打开我应在UITextView
中输入的文字的网址。我正在为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
答案 0 :(得分:1)
-(IBAction)ask
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.audaru.kz/?product=SoylemMT&word=%@",textView1.text]];
}
答案 1 :(得分:0)
如果我理解正确,您只想翻译在UITextField中输入的文本。所以你必须简单使用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: [NSString stringWithFormat:@"http://www.audaru.kz/?product=SoylemMT&word=%@", textView1.text]]];
将%@
移动到您网址中的正确位置。
希望它有所帮助
答案 2 :(得分:0)
试试这个
//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]];
答案 3 :(得分:0)
您可以从文本视图中获取URL:
NSString *urlString = self.textView.text;
然后你可以打开网址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
答案 4 :(得分:0)
我在代码中添加了注释以解释每个步骤,但您应该能够使用NSString *
构建stringWithFormat:
并将其传递给您(http://www.audaru.kz/?product=SoylemMT&word=%@
)的URL和值[[self textView1] text]
然后您需要做的就是将其传递给NSURL *
的实例并使用[[UIApplication sharedApplication] canOpenURL:]
检查您是否可以打开该网址。检查完毕并且如果是,则可以使用[[UIApplication sharedApplication] openURL:]
打开它。同时确保所有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