类方法之间匹配2个NSString值



我明白我不能在类方法中传递实例变量,所以我不再困惑两者之间的区别。

因此我有点卡住了。

我有两个类方法,它们都可以将NSString作为参数。

有什么办法可以把它们配对吗?因为一个Class方法有一个字符串,它将是一个url,需要在按下按钮后在Safari中打开因此@selector(openBrowser:)需要知道JWKObjectView01

的url是什么

请告诉我有办法做到这一点??

我已经尝试改变它所有的实例方法,但应用程序崩溃时,我按下按钮-所以我试图解决这个问题:-)

提前感谢。PS我知道我开始说,我明白你不能混合两个类-据我所知,但也许我错过了什么?

//添加代码:

UIView类. h文件

@interface JWKObjectView01 : UIView <UIWebViewDelegate>
{
    NSString *string;
    NSURL *url;
    NSUserDefaults *defaults;
}
+ (JWKObjectView01 *)anyView:(UIView *)anyView
                         title:(NSString *)title
                        weburl:(NSString *)webstring;
+ (void)openBrowser:(NSString *)urlString;

。m文件

+ (JWKObjectView01 *)anyView:(UIView *)anyView
                       title:(NSString *)title
                      weburl:(NSString *)webString
{
    JWKObjectView01 *anotherView = [[JWKObjectView01 alloc] initWithFrame:CGRectMake(0,0,320,200)];
    anotherView.backgroundColor = [UIColor yellowColor];
    [anyView addSubview:anotherView];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(20, 20, 100, 100);
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openBrowser:) forControlEvents:UIControlEventTouchUpInside];
    [anotherView addSubview:button];
    return anotherView;
}
+ (void)openBrowser:(NSString *)urlString;
{
    //This is where I am stuck and I need the variable - weburl:(NSString *)webString - 
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}

。m文件视图控制器

-(void)viewDidLoad
  {
     [JWKObjectView01 anyView:self.view title:@"OPEN" weburl:@"http://google.com"];
  }

使用静态变量作为url。在initialize方法中初始化它(不是init方法)。当然,您可以添加一个方法来设置静态变量的值。

静态变量和其他语言中的类变量一样,在运行时只存在一次。

但是它们不是类变量。当名称也用于其他类中的其他静态变量时,可能会出现命名冲突。因此,让自己熟悉单例模式,并在需要静态变量时考虑使用它。

有些人"滥用"应用程序委托对象作为全局属性值的容器。这可能不是"书外",但工作良好,是相当普遍的。不过,我相信你还是单身好得多。

所有这些都假设相关的URL在JWKObjectView01的所有实例中一次携带相同的值。

最新更新