使用 swift 中的按钮操作控制 Web 视图

Control web view with button action in swift

您好,我是 swift 的新手,如果我的代码一团糟,请见谅。

我想用 bin 和文本控制 web 视图 field.I 意思是我想做一些事情,如果我在文本字段中输入正确的词并单击按钮,按钮检查值,如果值是更正网络视图 向我显示网站或我给它的 link to.thanks

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textfield: UITextField!
    @IBOutlet weak var web: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }  

    @IBAction func Btn(sender: AnyObject) {

        if textfield.text == "Nature" {
            let requestnature = NSURLRequest(URL: NSURL(string: "https://www.google.com/imgres?imgurl=https%3A%2F%2Fwallpapercave.com%2Fwp%2Fu9AVLry.jpg&imgrefurl=https%3A%2F%2Fwallpapercave.com%2Fnature-wallpaper&docid=KHA0yQyMDxoeLM&tbnid=PSHFXiArDFPVBM%3A&vet=10ahUKEwi01Z_NkoTkAhUJ_KQKHSYbCqkQMwh7KAMwAw..i&w=1920&h=1080&hl=en&bih=635&biw=1239&q=nature%20wallpaper&ved=0ahUKEwi01Z_NkoTkAhUJ_KQKHSYbCqkQMwh7KAMwAw&iact=mrc&uact=8")!)
            web.loadRequest(requestnature)
        }     
    }   
}

2019-08-14 23:07:19.508 Download app[781:9061] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Txtfield.' * First throw call stack: ( 0 CoreFoundation 0x0000000101393e65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001030d6deb objc_exception_throw + 48 2 CoreFoundation 0x0000000101393aa9 -[NSException raise] + 9 3 Foundation 0x000000010175d9bb -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288 4 UIKit 0x0000000101d41320 -[UIViewController setValue:forKey:] + 88 5 UIKit 0x0000000101f6ff41 -[UIRuntimeOutletConnection connect] + 109 6 CoreFoundation 0x00000001012d44a0 -[NSArray makeObjectsPerformSelector:] + 224 7 UIKit 0x0000000101f6e924 -[UINib instantiateWithOwner:options:] + 1864 8 UIKit 0x0000000101d47eea -[UIViewController _loadViewFromNibNamed:bundle:] + 381 9 UIKit 0x0000000101d48816 -[UIViewController loadView] + 178 10 UIKit 0x0000000101d48b74 -[UIViewController loadViewIfRequired] + 138 11 UIKit 0x0000000101d492e7 -[UIViewController view] + 27 12 UIKit 0x0000000101c1fab0 -[UIWindow addRootViewControllerViewIfPossible] + 61 13 UIKit 0x0000000101c20199 -[UIWindow _setHidden:forced:] + 282 14 UIKit 0x0000000101c31c2e -[UIWindow makeKeyAndVisible] + 42 15 UIKit 0x0000000101baa663 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131 16 UIKit 0x0000000101bb0cc6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1760 17 UIKit 0x0000000101bade7b -[UIApplication workspaceDidEndTransaction:] + 188 18 FrontBoardServices 0x0000000104f9c754 -[FBSSerialQueue _performNext] + 192 19 FrontBoardServices 0x0000000104f9cac2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45 20 CoreFoundation 0x00000001012bfa31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 21 CoreFoundation 0x00000001012b595c __CFRunLoopDoSources0 + 556 22 CoreFoundation 0x00000001012b4e13 __CFRunLoopRun + 867 23 CoreFoundation 0x00000001012b4828 CFRunLoopRunSpecific + 488 24 UIKit 0x0000000101bad7cd -[UIApplication _run] + 402 25 UIKit 0x0000000101bb2610 UIApplicationMain + 171 26 Download app 0x00000001011b132d main + 109 27 libdyld.dylib 0x0000000103be692d start + 1 28 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

这看起来您之前可能尝试过为您的 UITextField 创建一个插座,但拼写错误,在界面生成器中,如果您右键单击文本字段,您应该会看到插座连接,可能有两个,一个是拼写错误和损坏。

删除此出口参考并重试,看看是否能解决您的问题。

或者您可能已经建立了插座连接,然后发现了拼写错误并在代码中更正了它。在这种情况下,移除插座并重新添加。您可以按住 CTRL 键从代码中的 IBOutlet 拖动到按钮,反之亦然以创建插座。

插座坏了。

您已经删除或重命名了控制器中的插座名称。 在 .storyboard 文件的 Connection Inspector.

中删除它