【问题标题】:Open URL with UIButton使用 UIButton 打开 URL
【发布时间】:2011-01-20 05:29:11
【问题描述】:

我希望你一切都好,我已经在 iphone 上工作了几个星期,目前我遇到了问题,尝试用 url 打开一个 safari,我有一个 json 文件,它带有一个动态的 url。

这里我留下代码。

- (void) loadFiles {

NSArray *_json = [[[self getDataFromJson] objectAtIndex:0] retain];
if ([_json count] > 0)
{
 for (int i = 0; i < [_json count]; i++)
 {
  NSDictionary *file = [_json objectAtIndex:i];

  UIImage *buttonImage = [UIImage imageNamed:@"btn.png"];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:buttonImage forState:UIControlStateNormal];

  [button addTarget:self action:@selector(openBrowser:) forControlEvents:UIControlEventTouchUpInside];

  //works but i have warnings
  button.tag = [file objectForKey:@"linkURL"] ;
  CGRect frame = CGRectZero;
  frame.size = buttonImage.size;
  button.frame = frame;

  NSString *name = [file objectForKey:@"name"];
  NSString *description = [file objectForKey:@"description"];


            //Create Box 

 }
}
}

- (void) openBrowser:(id)sender
{ 
 NSString *url = ((UIControl *) sender).tag;
 [[UIApplication sharedApplication]  openURL:[NSURL URLWithString:url] ];
}

我需要从 UIButtom 打开一个 URL 没有投诉。 任何建议或帮助谢谢。 干杯

【问题讨论】:

  • 标签属性是一个整数,你不能在那里存储一个URL。

标签: iphone objective-c iphone-sdk-3.0 ios4 cocos2d-iphone


【解决方案1】:

子类 UIButton 并添加一个属性来存储 URL。

// MyButton.h
@interface MyButton : UIButton {
    NSString *urlString_;
}

@property (nonatomic, retain) NSString *urlString;

@end



// MyButton.m
#import "MyButton.h"

@implementation MyButton

@synthesize urlString = urlString_;

- (void)dealloc {
    [self setUrlString:nil];
    [super dealloc];
}

@end

然后你继续你的代码:

#import "MyButton.h"
.
.
.
- (void) loadFiles {

    NSArray *_json = [[[self getDataFromJson] objectAtIndex:0] retain];
    if ([_json count] > 0)
    {
        for (int i = 0; i < [_json count]; i++)
        {
            NSDictionary *file = [_json objectAtIndex:i];

            UIImage *buttonImage = [UIImage imageNamed:@"btn.png"];
            MyButton *button = [MyButton buttonWithType:UIButtonTypeCustom];
            [button setImage:buttonImage forState:UIControlStateNormal];

            [button addTarget:self action:@selector(openBrowser:) forControlEvents:UIControlEventTouchUpInside];

            //works but i have warnings
            button.urlString = [file objectForKey:@"linkURL"] ;
            CGRect frame = CGRectZero;
            frame.size = buttonImage.size;
            button.frame = frame;

            NSString *name = [file objectForKey:@"name"];
            NSString *description = [file objectForKey:@"description"];


            //Create Box 

        }
    }
}

- (void) openBrowser:(id)sender {
    if ([sender isKindOfClass:[MyButton class]]) {
        [UIApplication sharedApplication] openURL:[NSURL URLWithString:[[(MyButton *)sender urlString]]];
    }
}

【讨论】:

  • 为了添加这个,子类 UIButton 可能有点过分了。不过+1,因为这也可以解决问题。
  • 我会说管理一个数组来映射按钮和 url 有点过头了。我想我太懒了!我宁愿子类化并在按钮和 URL 之间建立更强的关系。这有点迂腐,但我会节省保存 NSMutableDictionary 所需的内存。嗯,这只是我一开始想到的一个解决方案,像这样应该有几十个其他的解决方案可以更好或等于我提出的解决方案。
  • 糟糕,我说的 NSMutableDictionary 应该是 NSMutableArray。
【解决方案2】:

tag 是一个 int 类型。所以你不能在这里存储 NSString URL。您需要将 URL 存储在其他地方,可能在 NSMutableArray 中。将索引 i 存储在按钮的标签中,并将 URL 存储在位置 i 的 NSMutableArray 中。然后在按钮处理程序中使用标签作为索引从 NSMutableArray 获取 URL。

与问题无关,您没有发布_json NSArray。所以你正在泄漏内存。而且你也不需要测试if ([_json count] &gt; 0)。您的 for 循环已经在处理这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2013-10-19
    • 2016-01-09
    • 2014-06-10
    相关资源
    最近更新 更多