2016-11-23 5 views
1

내가 IOS 프로그래밍에 새로운 해요,하지만 난 그것이 스위프트 3touchUpInside 이벤트가

class BottomMenu : NSObject{ 

    //Container view 
    var bottomView: UIView!  

    //Button 
    var buttonContents : UIButton! 


    init(bottomView : UIView) { 
     super.init() 
     self.bottomView = bottomView   

     buttonContents = UIButton(frame: getFrame(index: 0))  
     buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen 
     bottomView.addSubview(buttonContents) 

     buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside) 
    } 

    func getFrame(index : CGFloat!) -> CGRect{ 
     let screenW = UIScreen.main.bounds.width 
     return CGRect(x: (screenW/3) * index, 
          y: 0, 
          width: screenW/3, 
          height: self.bottomView.frame.size.height) 
    }  
    func onClick(sender: UIButton!){ 
     NSLog("click") 
    } 

} 

의 를 작동하지 않는 이유는 어떤 생각을하지 않아도 이상하게도 동일한 코드가 다른 클래스에서 작동 트리거되지

let bottomMenu = BottomMenu(bottomView: bottomNavBarContainer) 

는 그래서, "클릭"결코 기록하지

+0

빠른 버전을 태그에 추가하십시오. – shallowThought

+0

예, 죄송합니다. Swift입니다. – user2976267

답변

2

귀하의 코드는 놀이터에서 잘 작동 보여줍니다 여기에이 클래스의 구현입니다. 오류는 클래스 사용에있어 야합니다. 부모보기는 userInteraction을 사용하지 않도록 설정되어 있습니다.

import UIKit 
import Foundation 
import PlaygroundSupport 

class BottomMenu : NSObject{ 

//Container view 
var bottomView: UIView! 

//Button 
var buttonContents : UIButton! 


init(bottomView : UIView) { 
    super.init() 
    self.bottomView = bottomView 

    buttonContents = UIButton(frame: getFrame(index: 0)) 
    buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen 
    bottomView.addSubview(buttonContents) 

    buttonContents.addTarget(self, action: #selector(onClick), for: .touchUpInside) 
} 

func getFrame(index : CGFloat!) -> CGRect{ 
    let screenW = UIScreen.main.bounds.width 
    return CGRect(x: (screenW/3) * index, 
        y: 0, 
        width: screenW/3, 
        height: self.bottomView.frame.size.height) 
} 
func onClick(sender: UIButton!){ 
    NSLog("click") 
} 

} 

var container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) 
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)) 
container.addSubview(view) 
let buttonContents = BottomMenu(bottomView: view) 

PlaygroundPage.current.liveView = container 
+0

해결되지 않은 식별자 'onClick'을 사용합니다. – user2976267

+0

예, 오류는 없지만 여전히 트리거되지 않았습니다. ( – user2976267

+0

대답이 – shallowThought

1

선택자 #selector(onClick)은 매개 변수가없는 기능을 나타냅니다. onClick 함수에는 단일 매개 변수가 있으므로 선택자는 #selector(onClick(sender:))이어야합니다. 적어도 스위프트 3의 선택기 형식이라고 생각합니다. 스위프트 선택기 구문에 여전히 익숙해 져 있습니다.

+0

업데이트 됨, 클릭 이벤트가 여전히 트리거되지 않음 – user2976267

+0

방금 ​​테스트 한 결과 문법을 설명했다. 다른 일이 생겨야한다. –

0

스위프트 3에 적응 시키려면 대상을 추가 할 때 두 가지 주요 변경 사항이 있습니다.

  1. 그냥 명확히하는 방법

    func onClick(_ sender: UIButton!){ 
    

에 강조하여 selector

buttonContents.addTarget(self, action: #selector(BottomMenu.onClick(_:)), for: .touchUpInside) 
  • 에 추가 변경

    :

    class BottomMenu : NSObject{ 
    
        //Container view 
        var bottomView: UIView!  
    
        //Button 
        var buttonContents : UIButton! 
    
    
        init(bottomView : UIView) { 
         super.init() 
         self.bottomView = bottomView   
    
         buttonContents = UIButton(frame: getFrame(index: 0))  
         buttonContents.backgroundColor = UIColor.blue //To see where to click on the screen 
         bottomView.addSubview(buttonContents) 
    
         buttonContents.addTarget(self, action: #selector(self.onClick(_:)), for: .touchUpInside) 
        } 
    
        func getFrame(index : CGFloat!) -> CGRect{ 
         let screenW = UIScreen.main.bounds.width 
         return CGRect(x: (screenW/3) * index, 
              y: 0, 
              width: screenW/3, 
              height: self.bottomView.frame.size.height) 
        }  
        func onClick(_ sender: UIButton!){ 
         NSLog("click") 
        } 
    
    } 
    
  • +0

    이 코드는 클릭을 유발하지 않는다 .. 다른 클래스에서는 잘 작동하지만 여기서는 .. – user2976267

    +1

    왜냐하면 할당 된'#selector()'가 잘못된 것입니다. 대답에 나열된 것들을 시도하십시오. 그게 효과가 있습니다. –

    +0

    이 오류는 런타임에 나타납니다. 그렇지 않으면 이유가 확실하지 않습니다. – Idan