2017-10-19 2 views
-1

Les boutons qui apparaissent dans la deuxième vue sont ajoutés par programmation. Comment le rendre invisible dans la vue?Les boutons apparaissent également dans la deuxième vue, même s'ils passent à différents ViewController

import UIKit 


    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

     @IBOutlet var mainScrollView: UIScrollView! 
     @IBOutlet weak var tableview: UITableView! 

     var Hotel_Name = [String]() 
     var itemView = UIView() 

     var identities = [String]() 


     override func viewDidLoad() { 
      super.viewDidLoad() 

//  let buttonAYŞE = Otel_1(text: "AYŞE") 
//  let buttonBREZKEMER = Otel_2(text: "BREZ KEMER") 
//   
//  buttonAYŞE.alpha = 0 
//  buttonBREZKEMER.alpha = 0 


      identities = ["listTable", "scrollview"] 


//****** Aşağıdaki kapalı olan JSONSerialization yapısı açıldığında web servisten çektiği verileri tableview'e aktarmaktadır. Üstteki HotelModel fonksiyonu kapatldığı taktirde. 
      let urlString = "http://service.ceylaner.com.tr/hotelapi/api/hotel/GetHotels?Company_id=1&Language_id=1" 
      let url = URL(string: urlString) 

      let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in 
       if error != nil{ 
        print(error!) 
       } 
       else 
       { 
        do 
        { 
         let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray 

         if let jsonDic = json 
         { 
          for i in 0..<jsonDic.count 
          { 
           if let basliklar = jsonDic[i] as? NSDictionary 
           { 
            if let Hotel_NameArray = basliklar["Hotel_Name"] as? NSString 
            { 
             self.Hotel_Name.append(Hotel_NameArray as String) 
            } 
           } 
          } 

          DispatchQueue.main.async{ 
          self.tableview?.reloadData() 
          } 
         } 
        } 
        catch 
        { 
         print(error) 
        } 
       } 
      } 
      task.resume() 

      view.addSubview(Otel_1(text: "AYŞE DFDFDNKFDFMKVFKVKNFVND")) 
      view.addSubview(Otel_2(text: "BREZ KEMER")) 

     } 
     //buttons 
     public func Otel_1(text:String) -> UIButton { 
      let btn_1 = UIButton(type: UIButtonType.system) 
      //Set a frame for the button. Ignored in AutoLayout/ Stack Views 
      btn_1.frame = CGRect(x: 10, y: 555, width: 170, height: 100) 
      //Set background color 
      btn_1.backgroundColor = UIColor.lightText 
      //State dependent properties title and title color 
      btn_1.setTitle(text, for: .normal) 
      btn_1.setTitleColor(UIColor.white, for: .normal) 
      btn_1.titleLabel?.numberOfLines = 2 
      btn_1.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping 
      btn_1.titleLabel?.textAlignment = NSTextAlignment.center 
      btn_1.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
      return btn_1 
     } 
     func Otel_2(text:String) -> UIButton { 
      let btn_2 = UIButton(type: UIButtonType.system) 
      //Set a frame for the button. Ignored in AutoLayout/ Stack Views 
      btn_2.frame = CGRect(x: 195, y: 555, width: 170, height: 100) 
      //Set background color 
      btn_2.backgroundColor = UIColor.lightText 
      //btn_1.backgroundColor = UIColor.blue 
      //State dependent properties title and title color 
      btn_2.setTitle(text, for: .normal) 
      btn_2.setTitleColor(UIColor.white, for: .normal) 
      btn_2.titleLabel?.numberOfLines = 2 
      btn_2.titleLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping 
      btn_2.titleLabel?.textAlignment = NSTextAlignment.center 
      btn_2.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
      return btn_2 
     } 

     func buttonAction(sender: UIButton!) { 
//   Button action'ı ile diğer Main viewcontroller'ından setting viewcontroller'ına geçer. 
//   let goBoard = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "InfoView") 
      var InfoView = self.storyboard!.instantiateViewController(withIdentifier: "InfoView") as! UIViewController 
      self.present(InfoView, animated: true, completion: nil) 
      InfoView.modalTransitionStyle = UIModalTransitionStyle.coverVertical // animasyon ekleme 

     } 


     // tableView Methodları 
     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return Hotel_Name.count 
     } 
     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
      let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! CustomCell 
      cell.InfoLabel.text = Hotel_Name[indexPath.row] 
      cell.InfoLabel.textColor = UIColor.white 
      cell.InfoImage.image = UIImage(named:"logo") 
      return cell 
     } 
     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
      return 150 
     } 
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      let vcName = identities[indexPath.row] 
      let viewController = storyboard?.instantiateViewController(withIdentifier: vcName) 
      self.navigationController?.pushViewController(viewController!, animated: true) 
     } 

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

    } 

Le bouton sur l'écran est cliqué.

enter image description here

L'écran 2 montrant les boutons sur le 1er écran.

enter image description here

J'ai créé le programme UIButton. Buttonaction est en cours. Les boutons 1 sur l'écran sont en cours de transfert vers le 2ème écran. Dois-je diviser les vues, je montre la mauvaise vue. Ou est-ce que l'action ajoute quelque chose en plus?

J'ai ajouté tout le code que j'ai écrit dans le ViewController.

+0

afficher la mise en œuvre de 'buttonAction' s'il vous plaît – Sweeper

+0

Comment allez-vous naviguait à l'écran suivant? – anoop4real

+0

S'il vous plaît lire [Dans quelles circonstances puis-je ajouter "urgent" ou d'autres phrases similaires à ma question, afin d'obtenir des réponses plus rapides?] (// meta.stackoverflow.com/q/326569) - le résumé est que ce n'est pas un moyen idéal de s'adresser aux volontaires, et est probablement contre-productif pour obtenir des réponses. Merci de ne pas ajouter ceci à vos questions. – halfer

Répondre

0

Lorsque vous écrivez comme

Otel_1(text: "AYŞE").alpha = 0 

vous créez en fait un nouveau bouton et à la fin, vous le cacher.

Chaque appel pour la fonction Otel_1 retourne une instance d'un nouveau bouton, donc comme une solution pour cela, vous pouvez déclarer les boutons à l'extérieur de la fonction d'élargir son champ d'application et durée de vie comme:

let btn_2 = UIButton(type: UIButtonType.system) 

puis l'utiliser dans la fonction

func Otel_2(text:String) -> UIButton { 
    btn_2.frame = CGRect(x: 195, y: 555, width: 170, height: 100) 
} 
+0

Merci Manar, pour toute votre aide, le problème a été résolu. –

0

vous pouvez définir alpha = 0 et il sera caché

+0

J'ai écrit 'Otel_1 (texte: "AYŞE"). Alpha = 0' dans buttonAction mais pas –

+0

non non, utilisez comme ceci: self.nextButton.alpha = 0, où nextButton est le nom de votre bouton – maku

+0

Merci pour votre aide J'écris comme ceci: 'self.Otel_1 (texte: "mybutton"). isHidden = true' et 'self.Otel_1 (texte: "mybutton"). alpha = 0' Mais, les boutons sont toujours visibles sur l'écran. J'ai spécifié 6 boutons dans une fonction séparée, et j'écris la fonction quand je tape 'alpha = 0'. Est-ce la bonne façon d'écrire? @eneaDume –