Swift2 在swift 2中,使用按钮循环代码块并递增数组

Swift2 在swift 2中,使用按钮循环代码块并递增数组,swift2,ios9,xcode7,Swift2,Ios9,Xcode7,我正在尝试制作一个简单的寻宝应用程序,利用estimote信标(我有3个信标)。所以我现在有一个带有按钮的主屏幕来开始搜寻;按下按钮时,下一个视图控制器是第一条线索。当设备距离第一个信标“近”时,屏幕会发生变化,指示用户正在靠近。当设备在附近时,会出现一个带有文本字段和“保存问题”按钮的问题。(所有这些都在一个视图控制器中完成,使用代码确定显示哪些项目以及何时显示)。用户回答问题,然后当他们单击“保存问题”时,他们的答案将保存到表视图中,并显示提示2。(我还没有做任何保存) 我的目标是再次循环。

我正在尝试制作一个简单的寻宝应用程序,利用estimote信标(我有3个信标)。所以我现在有一个带有按钮的主屏幕来开始搜寻;按下按钮时,下一个视图控制器是第一条线索。当设备距离第一个信标“近”时,屏幕会发生变化,指示用户正在靠近。当设备在附近时,会出现一个带有文本字段和“保存问题”按钮的问题。(所有这些都在一个视图控制器中完成,使用代码确定显示哪些项目以及何时显示)。用户回答问题,然后当他们单击“保存问题”时,他们的答案将保存到表视图中,并显示提示2。(我还没有做任何保存)

我的目标是再次循环。因此,我不想为每一轮线索-->接近-->问题创建一个新的视图控制器,而是想像以前一样返回到同一个视图控制器-我希望将来如果得到更多的信标,能够添加更多的问题/线索,所以我正试图找到最有效的方法(我希望这是有意义的!)。我觉得我需要一个for循环,但我不知道在哪里

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)

            }




    }

    }
让questionTitleArray:[字符串]=[“问题一”、“问题二”、“问题三”]
让clueTitleArray:[字符串]=[“线索一”、“线索二”、“线索三”]
让问题=[
11711:“皇帝和国王是哪种不会飞的鸟?”,
41848:“伦敦出租车的车顶上出现了什么发光的文字?”,
24952:“说出西尔维斯特·史泰龙扮演的虚构拳击手的名字”]
让线索:[字符串]=[“我是你的第一条线索”,“我是你的第二条线索”,“我是你的第三条线索”]
让我们提问:雷:[String]=[“Emporer和King是哪种不会飞的鸟?”,“伦敦出租车的车顶上出现了什么发光的字?”,“说出西尔维斯特·史泰龙扮演的虚构拳击手的名字”]
var temperatureImage:[字符串]=[“fireplace hi.png”]
让温度说:[String]=[“越来越热…”,“越来越冷…”,“冻结!”]
让minorValuesArray:[Int]=[117114184824952]
//变量i=0
重写func viewDidLoad(){
super.viewDidLoad()
self.image.alpha=0
self.textBox.alpha=0
self.button.alpha=0
变量i=0
questionLabel.text=clueTitleArray[i]
question.text=线索[i]
locationManager.delegate=self
if(CLLocationManager.authorizationStatus()!=CLAuthorizationStatus.AuthorizedWhenInUse)
{
locationManager.RequestWhenUseAuthorization()
}
StarTrangBeaconRegion(地区)位置经理
}
func locationManager(管理器:CLLocationManager,didRangeBeacons信标:[CLBeacon],区域内:CLBeaconRegion){
打印(信标)
通知nbeacons=beacons.filter
{
$0.0接近度!=CLP接近度。未知
}
如果(knownBeacons.count>0)
{
让closestBeacon=knownBeacons[0]作为CLBeacon
//插入for循环
变量i=0
//对于变量i=0;i
在beacon的项目,也许可以帮助你

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

}

在beacon的项目,也许可以帮助你

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

}

我认为你的方向是正确的。不需要循环;你只需要知道你在一系列问题/线索中的位置。把线索打包到他们自己的类中,开始传递这个类的实例。对不起,我对这个很陌生,所以不完全清楚你的意思。你的意思是只打包线索(就像在我的线索数组中一样),还是针对线索-->闭合-->问题的每个周期?是的,我猜;使用相同的视图控制器,将下一个索引传递给线索数组。有关如何替换视图控制器的示例实现,请参阅。我认为您的方向是正确的。不需要循环;你只需要知道你在一系列问题/线索中的位置。把线索打包到他们自己的类中,开始传递这个类的实例。对不起,我对这个很陌生,所以不完全清楚你的意思。你的意思是只打包线索(就像在我的线索数组中一样),还是针对线索-->闭合-->问题的每个周期?是的,我猜;使用相同的视图控制器,将下一个索引传递给线索数组。有关如何替换视图控制器的示例实现,请参见。