2014-06-08 21 views
10

Tôi có thể làm điều gì đó thực sự ngu ngốc, nhưng dường như tôi không thể sử dụng Trình tạo giao diện để kết nối các biến IBOutlet với chế độ xem tùy chỉnh, nhưng chỉ trong Swift.Swift, iboutlet và điều khiển tùy chỉnh

Tôi đã tạo một lớp có tên là MyView, mở rộng từ UIView. Trong bộ điều khiển của tôi, tôi đã có một biến MyView (được khai báo là @IBOutlet var newView: MyView). Tôi đi vào IB và kéo UIView vào cửa sổ và cho nó một lớp MyView.

Bất cứ khi nào tôi thực hiện tương tự trong Mục tiêu C, tôi có thể nhấp vào nút Xem bộ điều khiển ở đầu cửa sổ ứng dụng, chọn biến và kéo nó xuống điều khiển để liên kết hai. Khi tôi thử nó trong Swift, nó từ chối nhận ra rằng khung cảnh ở đó.

Nếu tôi thay đổi lớp biến trong bộ điều khiển thành UIView, nó hoạt động tốt. Nhưng không phải với chế độ xem tùy chỉnh của tôi.

Có ai khác gặp sự cố này không? Và nó là một tính năng, hay chỉ là sự ngu xuẩn của tôi?

Mã cho Controller

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet var newView:MyView 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

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

Mã để xem

import UIKit 

class MyView: UIView { 

    init(frame: CGRect) { 
     super.init(frame: frame) 
     // Initialization code 
    } 

    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) 
    { 
     // Drawing code 
    } 
    */ 

} 
+0

Tôi cũng gặp sự cố này, tôi nghĩ đây là lỗi và bạn nên báo cáo. –

Trả lời

11

Tôi đã có một vấn đề tương tự, và tôi nghĩ rằng đó là một phần một vấn đề bộ nhớ đệm và một phần chỉ là một/vấn đề Swift Xcode6. Bước đầu tiên tôi tìm thấy là bắt buộc để đảm bảo rằng tệp điều khiển chế độ xem .swift sẽ được tải trong Trình chỉnh sửa trợ lý khi chọn "tự động".

Với phát hiện Xcode rằng cả hai tệp đều được liên kết, đôi khi tôi có thể điều khiển-kéo từ chế độ xem/nút/v.v. từ IB đến tệp .swift, nhưng thường phải kéo từ vòng tròn trống trong máng của dòng @IBOutlet var newView:MyView đến chế độ xem mà tôi muốn nó khớp với.

Nếu bạn không thể tải tệp để tải trong Assistant Editor sau đó tôi thấy rằng làm những điều sau đây thường sẽ làm việc:

  1. Tháo lớp tùy chỉnh từ giao diện IB
  2. sạch dự án (cmd + K)
  3. Đóng/mở lại Xcode
  4. Có thể làm sạch lại?
  5. Thêm lớp tùy chỉnh lại quan điểm
  6. Hy vọng nó hoạt động :)

Nếu điều đó dường như giúp bạn có được một nửa chiều/không nơi nào thêm một nhận xét và tôi sẽ xem nếu nó gây nên bất cứ thứ gì tôi đã làm

+1

Cảm ơnKhông ai thực sự khắc phục sự cố trực tiếp (kéo từ @IBOutlet hiện tại sang chế độ xem tùy chỉnh trong cửa sổ IB), nhưng đã cung cấp phương pháp thay thế - kéo từ cửa sổ MyView trong IB vào trình chỉnh sửa trợ lý, tạo ra (và liên kết) IBOutlet có liên quan. Điều này dường như đạt được cùng một nhu cầu tổng thể. – hobart

+0

Hooking từ vòng tròn trống trong máng xối đã làm các trick! cảm ơn! – jomafer

3

Trong trường hợp của tôi import UIKit bị thiếu, sau khi thêm dòng này, tôi có thể tạo IBOutlet từ Bảng phân cảnh một lần nữa.

0

Tôi đã gặp sự cố tương tự với vấn đề được mô tả trong chuỗi này. Có thể bạn đã tìm ra một giải pháp có thể không, nhưng bất kỳ ai gặp phải điều này trong tương lai.Tôi đã tìm thấy chìa khóa là sử dụng "cần init" chức năng như sau:

required init(coder aDecoder: NSCoder) { 
    print("DrawerView: required init") 
    super.init(coder: aDecoder)! 
    screenSize = UIScreen.mainScreen().bounds 
    screenWidth = screenSize.width 
    screenHeight = screenSize.height 
    self.userInteractionEnabled = true 
    addCustomGestureRecognizer() 
} 

Đây là lớp học hoàn toàn giao diện tùy chỉnh của tôi:

nhập khẩu UIKit Foundation nhập khẩu

lớp DrawerView : UIView {

var screenSize: CGRect! 
var screenWidth: CGFloat! 
var screenHeight: CGFloat! 

var drawerState: Int = 0 

override init (frame : CGRect) { 
    print("DrawerView: main init") 
    super.init(frame : frame) 
} 

override func layoutSubviews() { 
    print("DrawerView: layoutSubviews") 
    super.layoutSubviews() 
} 

convenience init() { 
    self.init(frame:CGRect.zero) 
} 

required init(coder aDecoder: NSCoder) { 
    print("DrawerView: required init") 
    super.init(coder: aDecoder)! 
    screenSize = UIScreen.mainScreen().bounds 
    screenWidth = screenSize.width 
    screenHeight = screenSize.height 
    self.userInteractionEnabled = true 
    addCustomGestureRecognizer() 
} 

func addCustomGestureRecognizer(){ 
    print("DrawerView: addCustomGestureRecognizer") 
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:))) 
    swipeDown.direction = UISwipeGestureRecognizerDirection.Down 
    self.addGestureRecognizer(swipeDown) 
    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.handleDrawerSwipeGesture(_:))) 
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up 
    self.addGestureRecognizer(swipeUp) 
    print("DrawerView self: \(self)") 
} 

func minimizeDrawer(){ 
    UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: { 
     //   let height = self.bookButton.frame.size.height 
     //   let newPosY = (self.screenHeight-64)*0.89 
     //   print("newPosY: \(newPosY)") 
     self.setY(self.screenHeight*0.86) 
     }, completion: { finished in 
      self.drawerState = 0 
      for view in self.subviews { 
       if let _ = view as? UIButton { 
        let currentButton = view as! UIButton 
        currentButton.highlighted = false 
       } else if let _ = view as? UILabel { 
        let currentButton = view as! UILabel 
        if self.tag == 99 { 
         currentButton.text = "hisotry" 
        } else if self.tag == 999 { 
         currentButton.text = "results" 
        } 
       } 
      } 
    }) 
} 

func handleDrawerSwipeGesture(gesture: UIGestureRecognizer) { 
    print("handleDrawerSwipeGesture: \(self.drawerState)") 
    if let swipeGesture = gesture as? UISwipeGestureRecognizer { 
     switch self.drawerState{ 
     case 0: 
      if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down { 
       // nothing to be done, mini and swiping down 
       print("mini: !") 
      } else { 
       // mini and swiping up, should go to underneath city box 
       UIView.animateWithDuration(0.25, delay: 0.0, options: .CurveEaseOut, animations: { 
        let toYPos:CGFloat = 128 + 64 + 8 
        self.setY(toYPos) 
        }, completion: { finished in 
         self.drawerState = 1 
         for view in self.subviews { 
          if let _ = view as? UIButton { 
           let currentButton = view as! UIButton 
           currentButton.highlighted = true 
          } else if let _ = view as? UILabel { 
           let currentLabel = view as! UILabel 
           currentLabel.text = "close" 
          } 
         } 

       }) 
      } 
      break; 
     case 1: 
      if swipeGesture.direction == UISwipeGestureRecognizerDirection.Down { 
       // open and swiping down 
       self.minimizeDrawer() 
      } else { 
       // open and swiping up, nothing to be done 
      } 
      break; 
     default: 
      break; 
     } 
    } 
} 

}

Hy vọng điều này sẽ giúp ...

Các vấn đề liên quan