2017-04-13 155 views
0

我是iOS開發新手,使用xcode 8.2.1 & swift 3.我在視圖控制器中使用uitableview,在uitableviewcell中使用uiimageview當我點擊它時,應用程序崩潰並且當我採取按鈕並執行按鈕操作時,會發生同樣的問題。當我點擊uitableviewcell中的uiimageview時,應用程序崩潰

錯誤是:

sampleToRunBuild [3752:1620857] - [sampleToRunBuild.TapViewController TappedOnImage:]:無法識別的選擇發送到實例0x141e07bc0 2017年4月13日18:18:00.531126 sampleToRunBuild [3752 :1620857] * 終止應用程序由於未捕獲的異常 'NSInvalidArgumentException' 的,理由是: ' - [sampleToRunBuild.TapViewController TappedOnImage:]:無法識別 選擇發送到實例0x141e07bc0' *第一擲通話堆棧:(0x18343d1b8 0x181e7455c 0x183444268 0x183441270 0x18333a80c 0x1898b3f80 0x1898b7688 0x18947e73c 0x18931d0f0 0x1898a7680 0x1898a71e0 0x1898a649c 0x18931b30c 0x1892ebda0 0x189ad575c 0x189acf130 0x1833eab5c 0x1833ea4a4 0x1833e80a4 0x1833162b8 0x184dca198 0x1893567fc 0x189351534 0x100065f30 0x1822f95b8)的libC++ abi.dylib:與未捕獲 異常類型的終止NSException

我的代碼是:

import UIKit 

class TapViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate { 

    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.delegate = self 
     tableView.dataSource = self 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "mycell") 

     let img: UIImageView = cell?.viewWithTag(1) as! UIImageView 
     let img2: UIImageView = cell?.viewWithTag(2) as! UIImageView 

     img.tag = indexPath.row 
     img2.tag = indexPath.row 

     img.isUserInteractionEnabled = true 
     img2.isUserInteractionEnabled = true 

     let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:"))) 
     tapped.numberOfTapsRequired = 1 
     tapped.delegate = self 
     img.addGestureRecognizer(tapped) 


     let tapped1:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("TappedOnImage:"))) 
     tapped1.numberOfTapsRequired = 1 
     tapped1.delegate = self 
     img.addGestureRecognizer(tapped1) 

     return cell! 
    } 

    func TappedOnImage(sender:UITapGestureRecognizer){ 
     print("tap on imageview") 
    } 
} 

This is storyboard screenshot

在此先感謝...

回答

1

下面的代碼添加UITapGestureRecognizer

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))) 
    imageView.isUserInteractionEnabled = true 
    imageView.addGestureRecognizer(tapGestureRecognizer) 


func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) 
{ 
    let tappedImage = tapGestureRecognizer.view as! UIImageView 

    // Your action 
} 
+0

謝謝你這是我的工作 –

+0

歡迎你回覆我的回答@KunalMalve –

0

你需要使用

#selector(TapViewController.imageTapped(sender:)) 
0

如果您使用SWIFT 3,不要使用Selector()了,請改爲使用#selector語法。

例如

let tapped1:UITapGestureRecognizer = 
    UITapGestureRecognizer(target: self, action: #selector(TappedOnImage)) 

之所以你的代碼沒有工作是你忘了參數標籤sender添加到選擇的字符串。這是使用舊語法的缺點之一 - 它不會在編譯時告訴你錯誤。使用新的語法,如果您錯誤地編寫了選擇器,將會出現編譯器錯誤,而且您甚至不需要關心參數標籤,只需要名稱即可。

相關問題