2015-08-20 16 views
0

我剛剛開始學習Swift,我想做一個簡單的聊天應用程序。
我已經開始了聊天視圖,並且開始時,爲了讓我使用該語言,我希望能夠與自己進行對話。

例如,如果我寫的消息並按下send button以將其顯示在屏幕左側側,如果我寫的另一消息,並按下send button上的側顯示它在屏幕上,等等...

到目前爲止,我只是從我的input採取文字,把它變成一個label(和我做了我的框架和我的鍵盤響應)。

我不知道該怎麼做!? :
如何在swift中通過按下按鈕在視圖中添加多個標籤?

  • 我想我單擊該按鈕後,再拍label,最後一個後顯示它,在屏幕的另一部分...(CL:我要爲每一個消息拉布勒我送)

這裏是我的代碼到現在爲止,我已經在互聯網上搜索,但到目前爲止,我無法找到有用的東西......我會明白任何一種提示的...

謝謝!

    //my label 
        @IBOutlet weak var label: UILabel! 
        //my text field 
        @IBOutlet weak var textF: UITextField! 


        //we move the frame up and down with 250 when the keyboard appears 
        override func viewDidLoad() { 
         super.viewDidLoad() 
         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); 
         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); 
        } 

        //show keyboard 
        func keyboardWillShow(sender: NSNotification) { 
         self.view.frame.origin.y -= 250 
        } 


        //things TODO when the user is pressing the button 
        @IBAction func buttonPressed(sender: AnyObject) { 

         //create a variable(input) to store my text from the TextField 
         var input = textF.text 
         //put that text in the label 
         label.text = input 

         //empty text field 
         textF.text = " " 

         //create a new label for a new message 
         var label2 = UILabel(frame: CGRectMake(20, 20, 200, 21)) 

         // set the new label equal with the first to put the new message in it 
         //label2 = label 

         label2.center = CGPointMake(160, 284) 
         label2.textAlignment = NSTextAlignment.Center 
         label2.text = "I'am a test label" 



         //add it to the view 
         self.view.addSubview(label2) 


         //hide the keyboard 
         // self.textF.resignFirstResponder() 
        } 

        //hide keyboard if you tap the screen 
        override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
         self.view.endEditing(true) 
        } 

        //hide keyboard and get down the view 
        func keyboardWillHide(sender: NSNotification) { 
         self.view.frame.origin.y += 250 
        } 
+1

使用的tableView並使用2自定義tableViewCell一個與leftLabel另一個與rightLabel。當你按下按鈕時,添加你想要的單元格,然後重新加載單元格或tableview。你必須根據輸入文字動態設置單元高度 –

回答

1

*嘗試下面的代碼*

@IBOutlet weak var label: UILabel! 
//my text field 
@IBOutlet weak var textF: UITextField! 
var label2 : UILabel = UILabel(frame: CGRectMake(20, 20, 200, 21)) 
var isSendMessage: Bool = true { 
    didSet { 
     if isSendMessage { 
      var input = textF.text 
      label.text = input 
      textF.text = " " 

     } else { 
      var input = textF.text 
      label2.text = input 
      textF.text = " " 

     } 
    } 
} 


//we move the frame up and down with 250 when the keyboard appears 
override func viewDidLoad() { 
    super.viewDidLoad() 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil); 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil); 
    label2.center = CGPointMake(160, 284) 
    label2.textAlignment = NSTextAlignment.Center 
    self.view.addSubview(label2) 
} 

//show keyboard 
func keyboardWillShow(sender: NSNotification) { 
    self.view.frame.origin.y -= 250 
} 


//things TODO when the user is pressing the button 
@IBAction func buttonPressed(sender: AnyObject) { 

    isSendMessage = !isSendMessage 

    self.textF.resignFirstResponder() 
} 

//hide keyboard if you tap the screen 
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    self.view.endEditing(true) 
} 

//hide keyboard and get down the view 
func keyboardWillHide(sender: NSNotification) { 
    self.view.frame.origin.y += 250 
} 

} 
+0

謝謝!這有點幫助!我前進了一步......我不知道是不是'didSet'。 – flori

+0

它的行爲就像財產觀察員。當你設置這個變量時,SetSet調用 –

相關問題