【发布时间】:2013-04-18 10:22:23
【问题描述】:
我知道我不能在 Class 方法中传递实例变量,所以我不再混淆两者之间的区别。
因此我有点卡住了。
我有 2 个类方法,它们都可以将 NSString 作为参数。
到底有没有可以匹配的?
因为一个 Class 方法有一个字符串,该字符串是在按下按钮后需要在 Safari 中打开的 url,因此 @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,需要在按下按钮后在 Safari 中打开" - 如果没有代码示例,这部分很难理解:the talk about类变量“拥有”字符串听起来很可疑,因为类方法可以保持状态的唯一方法是通过其他类中的静态变量和单音。我怀疑问题出在那个区域的某个地方。请贴出类方法
A的相关代码。 -
好的 - 我已经添加了代码,希望对您有所帮助。
标签: ios objective-c class methods