我不想在每个UIViewController swift文件中使用相同的代码,所以我创建了一个自定义函数:
import Foundation;
import UIKit;
func noSignal(view : UIView) -> UIView {
let noConnectionView = UIView(frame: CGRectMake(100, 200, 260, 200));
noConnectionView.backgroundColor = UIColor.whiteColor();
noConnectionView.layer.cornerRadius = 10;
noConnectionView.layer.shadowColor = UIColor.blackColor().CGColor;
noConnectionView.layer.shadowRadius = 8;
noConnectionView.layer.shadowOpacity = 0.3;
noConnectionView.layer.shadowOffset = CGSizeMake(0, 0);
noConnectionView.center = view.center;
let heading = UILabel(frame: CGRectMake(10,40,240,20));
heading.textColor = UIColor.lightGrayColor();
heading.textAlignment = NSTextAlignment.Center;
heading.text = "NO CONNECTION";
noConnectionView.addSubview(heading);
let message = UITextView(frame: CGRectMake(10,80,240,60));
message.textColor = UIColor.lightGrayColor();
message.textAlignment = NSTextAlignment.Left;
message.text = "Please enable internet connection in your device (WiFi or Mobile) and tap retry.";
noConnectionView.addSubview(message);
let retry = UIButton(frame: CGRectMake(60,140,140,40));
retry.setTitle("Retry", forState: UIControlState.Normal);
retry.layer.cornerRadius = 6;
retry.backgroundColor = UIColor(netHex: 0xF91A6E);
retry.setTitleColor(UIColor.whiteColor(), forState: .Normal);
retry.addTarget(self, action: "retryConnection:", forControlEvents: UIControlEvents.TouchUpInside);
noConnectionView.addSubview(retry);
view.addSubview(noConnectionView);
return noConnectionView;
}
在所有示例中,我看到addTarget应该引用self,但是当我这样做时,Xcode显示使用uf未解析的标识符' self'所以我试图使用view然后它编译。但当我点击重试按钮时,它会崩溃。
所以我的问题是,我应该在哪里放置" retryConnection"功能,应该在noSignal函数内,还是在其他地方?此功能在UIWebView webViewDidStartLoad中运行。
答案 0 :(得分:0)
请尝试使用您要添加该视图的pass passcontroller并将目标更改为该viewcontroller而不是self
func noSignal(view : UIView , viewController : UIViewController) -> UIView {
let noConnectionView = UIView(frame: CGRectMake(100, 200, 260, 200));
noConnectionView.backgroundColor = UIColor.whiteColor();
noConnectionView.layer.cornerRadius = 10;
noConnectionView.layer.shadowColor = UIColor.blackColor().CGColor;
noConnectionView.layer.shadowRadius = 8;
noConnectionView.layer.shadowOpacity = 0.3;
noConnectionView.layer.shadowOffset = CGSizeMake(0, 0);
noConnectionView.center = view.center;
let heading = UILabel(frame: CGRectMake(10,40,240,20));
heading.textColor = UIColor.lightGrayColor();
heading.textAlignment = NSTextAlignment.Center;
heading.text = "NO CONNECTION";
noConnectionView.addSubview(heading);
let message = UITextView(frame: CGRectMake(10,80,240,60));
message.textColor = UIColor.lightGrayColor();
message.textAlignment = NSTextAlignment.Left;
message.text = "Please enable internet connection in your device (WiFi or Mobile) and tap retry.";
noConnectionView.addSubview(message);
let retry = UIButton(frame: CGRectMake(60,140,140,40));
retry.setTitle("Retry", forState: UIControlState.Normal);
retry.layer.cornerRadius = 6;
// retry.backgroundColor = UIColor(netHex: 0xF91A6E);
retry.setTitleColor(UIColor.whiteColor(), forState: .Normal);
retry.addTarget(viewController, action: "retryConnection:", forControlEvents: UIControlEvents.TouchUpInside);
noConnectionView.addSubview(retry);
view.addSubview(noConnectionView);
return noConnectionView;
}