2017-07-27 55 views
1

概述 - 我製作了一個flashcard應用程序。我已經到了用戶可以通過一系列圖像左右滑動的地步。圖像被分成11個不同的組,並且所有的組合都可以添加一個用戶可以滑過的最終數組(下面的代碼)。UIswitch在單獨視圖控制器中激活或停用圖像陣列

import UIKit 

class SecondViewController: UIViewController , UIGestureRecognizerDelegate { 

@IBAction func home(_ sender: Any) { 
    performSegue(withIdentifier: "home", sender: self) 
} 

@IBOutlet weak var imgPhoto: UIImageView! 

struct List { 
    let words: [String] 
    var active: Bool 
} 

let list1 = List(words:["lake", "lamb", "lamp", "lark", "leaf", "leash", "left", "leg", "lime", "lion", "lips", "list", "lock", "log", "look", "love", "lunch"], active: true) 

let list2 = List(words: ["ladder", "ladybug", "laughing", "lawnmower", "lemon", "leopard", "leprechaun", "letters", "licking", "lifesaver", "lifting", "lightbulb", "lightning", "listen", "llama"], active: true) 

let list3 = List(words: ["alligator", "balance", "ballerina", "balloon", "bowling", "cello", "colors", "curlyhair", "dollar", "dolphin", "elephant", "eyelashes", "gasoline", "goalie", "hula", "jellyfish", "olive", "pillow", "pilot", "polarbear", "rollerskate", "ruler", "silly", "telephone", "television", "tulip", "umbrella", "valentine", "violin", "xylophone", "yellow"], active: true) 

let list4 = List(words: ["apple", "ball", "bell", "bubble", "castle", "fall", "fishbowl", "girl", "owl", "pail", "peel", "pool", "smile", "whale", "wheel"], active: true) 

let list5 = List(words: ["planet", "plank", "plant", "plate", "play", "plum", "plumber", "plus"], active: true) 

let list6 = List(words: ["black", "blanket", "blender", "blocks", "blond", "blood", "blow", "blue"], active: true) 

let list7 = List(words: ["flag", "flipflop", "float", "floor", "flower", "fluffy", "flute", "fly"], active: true) 

let list8 = List(words: ["glacier", "glad", "glasses", "glide", "glitter", "globe", "glove", "glue"], active: true) 

let list9 = List(words: ["clam", "clamp", "clap", "claw", "clean", "climb", "clip", "cloud"], active: true) 

let list10 = List(words:["sled", "sleep", "sleeves", "slice", "slide", "slime", "slip", "slow"], active: true) 

let list11 = List(words: ["belt", "cold", "dolphin", "elf", "golf", "melt", "milk", "shelf"], active: true) 

var imageIndex: Int = 0 

var imageList: [String] { 

    let wordLists = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11] 



    let active = wordLists.reduce([]) { (result:[String], list:List) in 
     if list.active { 
      return result + list.words 

     } else { 
      return result 
     } 
    } 

    return active 

} 




override func viewDidLoad() { 
    super.viewDidLoad() 

    imgPhoto.image = UIImage(named: imageList[imageIndex]) 

    // Do any additional setup after loading the view. 
    imgPhoto.isUserInteractionEnabled = true 

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:))) 
    leftSwipe.cancelsTouchesInView = false 

    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(Swiped(gesture:))) 
    rightSwipe.cancelsTouchesInView = false 

    leftSwipe.direction = .left 
    rightSwipe.direction = .right 

    view.addGestureRecognizer(leftSwipe) 
    view.addGestureRecognizer(rightSwipe) 

} 

func Swiped(gesture: UIGestureRecognizer) { 

    if let swipeGesture = gesture as? UISwipeGestureRecognizer { 

     switch swipeGesture.direction { 

     case UISwipeGestureRecognizerDirection.right : 
      print("User swiped right") 

      // decrease index first 

      imageIndex -= 1 

      // check if index is in range 

      if imageIndex < 0 { 

       imageIndex = imageList.count - 1 

      } 

      imgPhoto.image = UIImage(named: imageList[imageIndex]) 

     case UISwipeGestureRecognizerDirection.left: 
      print("User swiped Left") 

      // increase index first 

      imageIndex += 1 

      // check if index is in range 

      if imageIndex > imageList.count - 1 { 

       imageIndex = 0 

      } 

      imgPhoto.image = UIImage(named: imageList[imageIndex]) 

     default: 
      break //stops the code/codes nothing. 
     } 
    } 
} 
} 

現在我正在設置頁面(下圖)。上面代碼中的11個單詞列表中的每一個都有1個開關。最上面的開關會控制列表1中,第二開關將控制列表2等等

enter image description here

的問題 - 是我想將功能添加到每個交換機。當開關處於關閉位置時,與其關聯的圖像組不應包含在最終陣列中,並且在用戶滑過閃卡時不會顯示。到目前爲止,我的設置頁面的代碼如下所示。我嘗試過試驗不同的代碼,比如將開關連接到ViewController並添加覆蓋函數,但我不確定此時該去哪裏。

import UIKit 

protocol WordSelectionDelegate: class { 
func wordSelected(newWord: Word) 
} 

class MasterViewController: UITableViewController { 
var words = [Word]() 

weak var delegate: WordSelectionDelegate? 

override func prepare(for segue: UIStoryboardSegue, sender: SecondViewController) { 
    if let vc = segue.destination as? MasterViewController { 
     vc.wordLists = wordLists 
    } 
} 


@IBAction func switchAction(_ sender: UISwitch) { 
    wordList[sender.tag].active = rollIntoLoanSwitch.isOn 
} 



override func viewDidLoad() { 
    super.viewDidLoad() 



} 
required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder)! 

    self.words.append(Word(name: "initial /l/ 1 syllable", description: "lake lamb lamp lark leaf leash left leg lime lion lips list lock log look love lunch")) 

    self.words.append(Word(name: "initial /l/ multisyllabic", description: "")) 

    self.words.append(Word(name: "intersyllabic /l/", description: "")) 

    self.words.append(Word(name: "final /l/", description: "")) 

    self.words.append(Word(name: "initial /pl/", description: "")) 

    self.words.append(Word(name: "initial /bl/", description: "")) 

    self.words.append(Word(name: "initial /fl/", description: "")) 

    self.words.append(Word(name: "initial /gl/", description: "")) 

    self.words.append(Word(name: "initial /kl/", description: "")) 

    self.words.append(Word(name: "initial /sl/", description: "")) 

    self.words.append(Word(name: "final /l/ clusters", description: "")) 


} 


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

// MARK: - Table view data source 

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return self.words.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    // Configure the cell... 
    let word = self.words[indexPath.row] 
    cell.textLabel?.text = word.name 
    return cell 
} 

override func tableView(_ tableView: UITableView, didSelectRowAt 
    indexPath: IndexPath) { 
    let selectedMonster = self.words[indexPath.row] 
    self.delegate?.wordSelected(newWord: selectedMonster) 
    if let Detail = self.delegate as? Detail { 
     splitViewController?.showDetailViewController(Detail, sender: nil) 
    } 

} 
/* 
// Override to support conditional editing of the table view. 
override func tableView(_ tableView: UITableView, can 

任何想法如何解決這個問題將不勝感激。謝謝

enter image description here

+0

我無法在代碼中看到被設置的UISwitch的委託/操作目標,您需要這樣做否則'switchAction'方法將永遠不會被調用。你在代碼中的其他任何地方做過嗎?如果你在該方法中設置了一個斷點,它是否曾被稱爲? – Mrwerdo

+0

我想我沒有設置。設置頁面是一個分割視圖控制器,所以還有其他類與分割視圖相關聯,但我沒有將切換操作方法放在除上述之外的任何其他類中。 –

回答

0

你錯過了一些零件到您的程序,使這項工作。它們是:

  1. 設置UISwitch控件的目標方法。
  2. 設置UISwitch控件的標籤號。
  3. 刷新SecondViewController中顯示的圖像。

要設置UISwitch控制的目標的方法,你就需要調用addTarget(_:action:for:)每個交換機上,通過引用,你有MasterViewController。如果你這樣做,那麼我建議在Interface Builder中爲你的UISwitch控件設置一個標籤。例如,假設你UISwitch具有1標記:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    // Configure the cell... 
    let word = self.words[indexPath.row] 
    cell.textLabel?.text = word.name 
    if let switchControl = cell.viewWithTag(1) as UISwitch { 
     switchControl.addTarget(self, action: #selector(switchAction(_:)), for: .valueChanged) 
    } 

    return cell 
} 

對於點3中,數據在MasterViewController比在SecondViewController不同。您需要獲得對SecondViewController的引用,然後更新那裏的數據,並告訴它重新顯示所選圖像。有關如何從分割視圖控制器訪問對等視圖控制器,請參閱this question for details

+0

謝謝。但是,閃存卡不會顯示在「詳細信息」視圖控制器中。我將在那裏顯示其他信息。我爲故事板添加了一張照片,以便您可以看到該應用的佈局。與貓頭鷹的場景是開幕畫面,然後是閃卡(具有鱷魚圖像的場景,這是SecondViewController)的延續。在右邊你可以看到我的分割視圖控制器,這是我正在處理的設置頁面。所以我想將MainViewController(設置頁面)的信息傳遞給SecondViewController(鱷魚場景) –

相關問題