1

I am trying to make a simple treasure hunt app that makes use of estimote beacons (I have 3 beacons). So I currently have a home screen with a button to start the hunt; when the button is pressed, the next view controller is the first clue. When the device is within 'near' proximity to first beacon, the screen changes to indicate that the user is getting closer. When the device is in the immediate proximity, a question appears with a text field and 'save question' button. (All of this is done in one view controller, using code to determine what items are shown and when). The user answers the question, then when they click 'save question', their answer is saved to a table view, and clue 2 appears. (I've not done any of the saving yet)

My aim is to loop through again. So instead of creating a new view controller for each 'round' of clue-->getting close-->question, I want to loop back to the same view controller as before - I want to be able to add more questions/clues in the future if I get more beacons, so I'm trying to find the most efficient way of doing it (I hope that makes sense!). I feel like I need a for loop somewhere, but I can't figure out where.

let questionTitleArray: [String] = ["Question One", "Quesiton Two", "Question Three"]

let clueTitleArray: [String] = ["Clue One", "Clue Two", "Clue Three"]

let questions = [
    11711: "Emporer and King are types of which flightless bird?",
    41848: "What illuminated word appears on the roof of a London cab?",
    24952: "Name the fictional boxer played by Sylvester Stallone"]

let clues: [String] = ["I'm your first clue", "I'm your second clue", "I'm your third clue"]

let questionsArray: [String] = ["Emporer and King are types of which flightless bird?", "What illuminated word appears on the roof of a London cab?", "Name the fictional boxer played by Sylvester Stallone"]

var temperatureImage: [String] = ["fireplace-hi.png"]

let temperatureArray: [String] = ["Getting warmer...", "Getting colder...", "Freezing!"]

let minorValuesArray:[Int] = [11711, 41848, 24952]


//var i = 0

override func viewDidLoad() {
    super.viewDidLoad()
    self.image.alpha = 0
    self.textBox.alpha = 0
    self.button.alpha = 0

    var i = 0

    questionLabel.text = clueTitleArray[i]

    question.text = clues[i]

    locationManager.delegate = self

    if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse)
    {
       locationManager.requestWhenInUseAuthorization()
    }
    locationManager.startRangingBeaconsInRegion(region)



}



func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
    print(beacons)



    let knownBeacons = beacons.filter
        {
            $0.proximity != CLProximity.Unknown
        }

    if (knownBeacons.count > 0)
    {

        let closestBeacon = knownBeacons[0] as CLBeacon

        //insert a for loop
        var i = 0

        //for var i = 0; i < minorValuesArray.count; i++
        //while i < minorValuesArray.count

        if closestBeacon.minor.integerValue == minorValuesArray[i] && closestBeacon.proximity == CLProximity.Near //if the beacon is purple (clue1) and proximity is near, hide the labels and just show image
        {

            self.questionLabel.text = temperatureArray[0]
            self.button.alpha = 0
            self.textBox.alpha = 0
            self.image.alpha = 1
            self.question.alpha = 0



        }

            else if closestBeacon.minor.integerValue == minorValuesArray[0] && closestBeacon.proximity == CLProximity.Immediate //if the beacon is purple and proximity is immediate (user is in the right place), show aa success message and the next question.  Stop ranging so the screen doesn't flicker back to warm.
            {
                self.image.alpha = 0
                self.questionLabel.alpha = 1
                self.question.alpha = 1


                self.questionLabel.text = self.questionTitleArray[i]

                self.question.text = self.questionsArray[i]

                self.textBox.alpha = 1

                self.button.alpha = 1

                locationManager.stopRangingBeaconsInRegion(region)

            }




    }

    }
Laura
  • 11
  • 1
  • I think you're going in the right direction. No looping is required; you simply need to know where you are in the chain of questions/clues. Package up the clues into their own class and start passing instances of this class around. – l00phole Oct 16 '15 at 14:12
  • Sorry I'm very new to this so not entirely sure what you mean. Do you mean package just the clues (as in from my clues array), or for each cycle of clue-->closer-->question? – Laura Oct 16 '15 at 14:43
  • Yeah I guess; use the same view controller, passing it the next index into the clue array. See [this question](http://stackoverflow.com/questions/21414786/instead-of-push-segue-how-to-replace-view-controller-or-remove-from-navigation) for a sample implementation of how to replace the view controller. – l00phole Oct 16 '15 at 14:58

1 Answers1

0

project at beacon, maybe help you

import UIKit
import CoreLocation
import MediaPlayer

class OneViewController: UIViewController {
//定义答对图像开始
var goodImg = UIImageView()

//定义答对图像结束
var currentQuestion = 0

let allQuestions = [
    ["ベートーヴェンは何年に生まれたでしょうか","1770年","1868年","1776 年"],
    ["トランペットを吹くために注意した方がいいことは何でしょうか?","マウスピースをくわえる","よく聞こえるように人の近くで吹く","手の形をテニスボールを持っているようにする"],
    ["NaHCO3は何の化学式でしょうか?","酢","重奏","溶岩"]
]

let rightAnswer = [1]

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
    let backImageView = UIImageView()
    backImageView.frame = CGRectMake(0, 0, 1024, 768)
    let img = UIImage(named: "mainBgImg.png")
    backImageView.image = img
    self.view.addSubview(backImageView)


    let questionLabel = UILabel()
    questionLabel.frame = CGRectMake(595, 265, 370, 30)
    questionLabel.text = allQuestions[0][0]
    questionLabel.textColor = UIColor.whiteColor()
    questionLabel.font = UIFont.systemFontOfSize(18)
    questionLabel.textAlignment = .Center
    self.view.addSubview(questionLabel)

    //在主页面中添加按钮并且设置位置
    let questionBtnA = UIButton()
    questionBtnA.frame = CGRectMake(595, 310, 370, 68)
    questionBtnA.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnA)
    questionBtnA.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnA.tag = 1

    let questionBtnB = UIButton()
    questionBtnB.frame = CGRectMake(595, 400, 370, 68)
    questionBtnB.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnB)
    questionBtnB.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnB.tag = 2

    let questionBtnC = UIButton()
    questionBtnC.frame = CGRectMake(595, 490, 370, 68)
    questionBtnC.setImage(UIImage(named: "mainBtn"), forState: .Normal)
    self.view.addSubview(questionBtnC)
    questionBtnC.addTarget(self, action: "rightResult:", forControlEvents: .TouchUpInside)
    questionBtnC.tag = 3

    //在住页面中添加按钮标签并设置位置
    let buttonLabel1 = UILabel()
    buttonLabel1.frame = CGRectMake(595, 310, 370, 68)
    buttonLabel1.textAlignment = .Center
    buttonLabel1.text = allQuestions[0][1]
    buttonLabel1.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel1)

    let buttonLabel2 = UILabel()
    buttonLabel2.frame = CGRectMake(595, 400, 370, 68)
    buttonLabel2.textAlignment = .Center
    buttonLabel2.text = allQuestions[0][2]
    buttonLabel2.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel2)

    let buttonLabel3 = UILabel()
    buttonLabel3.frame = CGRectMake(595, 490, 370, 68)
    buttonLabel3.textAlignment = .Center
    buttonLabel3.text = allQuestions[0][3]
    buttonLabel3.font = UIFont.systemFontOfSize(20)
    self.view.addSubview(buttonLabel3)



}

func doneButtonClick(sender:NSNotification?){
    //self.dismissMoviePlayerViewControllerAnimated()
    self.dismissViewControllerAnimated(true, completion: nil)
    print("aa")
}


func rightResult(sender:UIButton){
    if sender.tag == rightAnswer[currentQuestion]{
        let imagesccuss = UIButton()
        imagesccuss.frame = CGRectMake(555, 230, 450, 370)
        imagesccuss.setImage(UIImage(named: "yes"), forState: .Normal)
        self.view.addSubview(imagesccuss)
        //答对图案开始
        goodImg.image = UIImage(named: "good.png")
        goodImg.frame = CGRectMake(380, 605, 50, 40)
        self.view.addSubview(goodImg)

        UIImageView.animateWithDuration(2, delay: 0,usingSpringWithDamping:0.5,initialSpringVelocity:0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            self.goodImg.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(2, 2), CGAffineTransformMakeTranslation(250, 50))
            }, completion: nil)

        //答对图案结束
        imagesccuss.addTarget(self, action:"turnBack",forControlEvents:.TouchUpInside)

    }else{
        let imagesccuss = UIButton()
        imagesccuss.frame = CGRectMake(555, 230, 450, 370)
        imagesccuss.setImage(UIImage(named: "error"), forState: .Normal)
        self.view.addSubview(imagesccuss)
        imagesccuss.addTarget(self, action:"errorBack",forControlEvents:.TouchUpInside)
    }
}

func turnBack() {
    let avc = messagesTwoViewController()
    self.presentViewController(avc, animated: true, completion: nil)
    //self.dismissViewControllerAnimated(true, completion:nil)
}



func errorBack() {
    let vc = OneViewController()
    self.presentViewController(vc, animated: false, completion: nil)
    //self.dismissViewControllerAnimated(true, completion:nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidDisappear(animated: Bool) {
}

}

Lop Hu
  • 1,443
  • 2
  • 9
  • 8