2014-06-08 13 views
10

È possibile che stia facendo qualcosa di veramente stupido, ma non riesco a utilizzare Interface Builder per connettere le variabili IBOutlet alle viste personalizzate, ma solo in Swift.Controlli Swift, iboutlet e personalizzati

Ho creato una classe chiamata MyView, che si estende da UIView. Nel mio controller, ho una variabile MyView (dichiarata come @IBOutlet var newView: MyView). Vado in IB e trascino un UIView sulla finestra e gli do una classe di MyView.

Ogni volta che ho eseguito un simile obiettivo in Objective C, sono quindi in grado di fare clic sul pulsante Visualizza controller nella parte superiore della finestra dell'app, selezionare la variabile e trascinarla sul controllo per collegare i due insieme. Quando lo provo in Swift, si rifiuta di riconoscere che la vista è lì.

Se cambio la classe della variabile nel controller su UIView, funziona correttamente. Ma non con la mia vista personalizzata.

Qualcun altro ha avuto questo problema? Ed è una caratteristica, o solo la mia idiozia?

Codice per il regolatore

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. 
    } 
} 

Codice in vista

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

Anche io sto riscontrando questo problema, penso che questo sia un bug e dovresti segnalarlo. –

risposta

11

ho avuto un problema simile, e penso che sia in parte un problema di caching e parzialmente solo un problema Xcode6/Swift. Il primo passo che ho trovato è stato quello di assicurarmi che il file .swift del controller di visualizzazione fosse caricato nell'Assistente Assistente quando scegli "automatico".

Con Xcode che rileva che entrambi i file sono collegati, a volte potrei controllare-trascinare dalla vista/pulsante/ecc. dall'IB al file .swift, ma spesso dovevo trascinare dal cerchio vuoto nella canalina della linea @IBOutlet var newView:MyView alla vista che volevo corrispondesse fino a.

Se non è possibile ottenere il file da caricare nel Assistant Editor poi ho scoperto che facendo seguito Spesso lavorare:

  1. Rimuovere la classe personalizzata dal punto di vista IB
  2. Pulire il progetto (cmd + K)
  3. Chiudi/riapri Xcode
  4. Forse pulire di nuovo?
  5. Aggiungere la classe personalizzata tornare alla vista
  6. spero che funziona :)

Se questo sembra per arrivare a metà strada/nulla aggiungere un commento e vedrò se si innesca ogni altra cosa che ho fatto

+1

Grazie. Nessuno ha risolto il problema diretto (trascinando da un @IBOutlet esistente alla visualizzazione personalizzata nella finestra IB), ma ha fornito un approccio alternativo - trascinando dalla finestra MyView in IB nell'editor degli assistenti, che crea (e collegamenti) l'IBOutlet pertinente. Questo sembra raggiungere lo stesso bisogno generale. – hobart

+0

L'aggancio dal cerchio vuoto nella grondaia ha fatto il trucco! Grazie! – jomafer

3

Nel mio caso mancava import UIKit, dopo aver aggiunto questa riga, potevo creare di nuovo un IBOutlet dallo Storyboard.

0

Ho avuto un problema simile a quello descritto in questa discussione. Forse hai trovato una soluzione forse no, ma chiunque incontri questo in futuro.Ho trovato la chiave è quella di utilizzare la funzione "necessaria init" come segue:

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() 
} 

Questa è la classe completa della mia vista personalizzata:

importazione UIKit importazione Fondazione

classe 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; 
     } 
    } 
} 

}

Spero che questo aiuti ...

Problemi correlati