Swift:可能的逻辑错误

时间:2016-10-06 23:44:47

标签: swift logic

我正在尝试创建一个简单的tic tac toe游戏。不需要人工智能。我已经说到我可以点击我的UI中的按钮,他们每次都会切换。我还创建了一个checkScore()函数。但是,我认为我的checkScore()函数在某处有错误的逻辑,我花了两天时间试图找出它。如果用户连续获得三个,那么它应该运行winnerMessage()函数以获得胜利。我的变量名称非常自我解释。

我的问题是,除非我在checkScore()函数中注释掉所有“else if”语句,否则winnerMessage会在第一次单击该板时运行。请帮忙!感谢。

定义变量。

    var player1 = "❌"
    var player2 = "⭕️"
    var currentPlayer = "❌"

    let originalValue = ""

    let winnerMessageTopHorizontal = "We have a winner across the top!"
    let winnerMessageCenterHorizontal = "We have a winner across the  center!"
    let winnerMessageBottomHorizontal = "We have a winner across the bottom!"
    let winnerMessageLeftVertical = "We have a winner on the left side!"
    let winnerMessageCenterVertical = "We have a winner down the center!"
    let winnerMessageRightVertical = "We have a winner on the right side!"
    let winnerMessageDiagonal1 = "We have a winner diagonally!"
    let winnerMessageDiagonal2 = "We have a winner diagonally!"

这是我创建的changePlayer()函数。

/* function that changes from player1 to player2 (switching from X to O and O to X)*/
    func changePlayer(){

        if currentPlayer == player1{
            print ("\(player1)")
            currentPlayer = player2
        }



        else if currentPlayer == player2{
            print ("\(player2)")
            currentPlayer = player1
        }


        else{
            NSLog ("Error. Please reset the game.")
        }

    }

这是我一直遇到问题的checkScore()函数。如果你注释掉除第一个函数之外的所有函数行,那么你可以看到第一步是如何工作的。

func checkScore(){



   if topLeftOutlet.currentTitle == topCenterOutlet.currentTitle && topLeftOutlet.currentTitle == topRightOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageTopHorizontal
        }

        else if leftCenterOutlet.currentTitle == centerOutlet.currentTitle && leftCenterOutlet.currentTitle == rightCenterOutlet.currentTitle && leftCenterOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageCenterHorizontal
        }
        else if bottomLeftOutlet.currentTitle == bottomCenterOutlet.currentTitle && bottomLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && bottomLeftOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageBottomHorizontal
        }
        else if topLeftOutlet.currentTitle == leftCenterOutlet.currentTitle && topLeftOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageLeftVertical
        }
        else if topCenterOutlet.currentTitle == centerOutlet.currentTitle && topCenterOutlet.currentTitle == bottomCenterOutlet.currentTitle && topCenterOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageCenterVertical
        }
        else if topRightOutlet.currentTitle == rightCenterOutlet.currentTitle && topRightOutlet.currentTitle == bottomRightOutlet.currentTitle && topRightOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageRightVertical
        }
        else if topLeftOutlet.currentTitle == centerOutlet.currentTitle && topLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageDiagonal1
        }
        else if topRightOutlet.currentTitle == centerOutlet.currentTitle && topRightOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet.currentTitle != ""{
            announcementArea.text = winnerMessageDiagonal2
        }

        else{
            NSLog ("Keep going")
        }




    }
     @IBOutlet weak var announcementArea: UITextField!

电路板上按钮的操作和插座。操作是当用户单击按钮时它将更改为X或O.出口用于下面的重置功能,将值重置为空。

    @IBAction func topLeft(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var topLeftOutlet: UIButton!

    @IBAction func topCenter(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var topCenterOutlet: UIButton!

    @IBAction func topRight(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var topRightOutlet: UIButton!

    @IBAction func leftCenter(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var leftCenterOutlet: UIButton!

    @IBAction func center(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var centerOutlet: UIButton!

    @IBAction func rightCenter(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var rightCenterOutlet: UIButton!

    @IBAction func bottomLeft(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }

    @IBOutlet weak var bottomLeftOutlet: UIButton!

    @IBAction func bottomCenter(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var bottomCenterOutlet: UIButton!

    @IBAction func bottomRight(sender: UIButton) {
        sender.setTitle(currentPlayer, forState: .Normal)
        changePlayer()
        checkScore()
    }
    @IBOutlet weak var bottomRightOutlet: UIButton!

此按钮目前无用:

    @IBAction func startGame(sender: UIButton) {
        NSLog ("Player Started Game")
    }

resetButton()函数,当玩家点击重置按钮时重置棋盘

    @IBAction func resetGame(sender: UIButton) {
        NSLog ("Player Reset Game")
        topLeftOutlet.setTitle(originalValue, forState: .Normal)
        topCenterOutlet.setTitle(originalValue, forState: .Normal)
        topRightOutlet.setTitle(originalValue, forState: .Normal)
        leftCenterOutlet.setTitle(originalValue, forState: .Normal)
        centerOutlet.setTitle(originalValue, forState: .Normal)
        rightCenterOutlet.setTitle(originalValue, forState: .Normal)
        bottomLeftOutlet.setTitle(originalValue, forState: .Normal)
        bottomCenterOutlet.setTitle(originalValue, forState: .Normal)
        bottomRightOutlet.setTitle(originalValue, forState: .Normal)
    }


}

非常感谢你看一看。我不指望它是完美的。我只需要另一只眼睛看看我是否错过了一些明显的东西!

2 个答案:

答案 0 :(得分:0)

我找出你的问题。问题在于changeScore方法,你的按钮在开始时包含nil值,但你将它们与“”字符串进行比较,这就是为什么每次你的条件总是为真并显示赢家Message.you只需要替换所有你的条件!=“”有!= nil喜欢这个

<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
     <script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <style type='text/css'>
        @font-face {
            font-family: 'Pacifico';
            font-style: normal;
            font-weight: 400;
            src: local('Pacifico Regular'), local(Pacifico-Regular), url(font/Pacifico-Regular.woff2) format("woff2");
            unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000
        }
    </style>

    <!--
    <link rel="stylesheet" type="text/css" href="../css/jquery-ui.min.css" />-->
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]--><!--

    <script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
    <script type="text/javascript" src="../js/jquery-ui.min.js"></script>
    <link rel="stylesheet" href="../css/datepicker/jquery-ui.css">
    <script src="../js/datepicker/jquery-1.12.4.js"></script>
    <script src="../js/datepicker/jquery-ui.js"></script>
    -->
    <script type="text/javascript">
        function readyDatePicker1() {
          $(function() {
            $("#tgl").datepicker({
                //minDate: -1, maxDate: "+1D",
                changeMonth: true,
                changeYear: true,
                todayBtn: "linked",
                language: "id",
                autoclose: true,
                todayHighlight: true,
                dateFormat: 'ddMyy'
            });
          });
        }

        function readyDatePicker2(){
          $(function() {
            $("#tgl2").datepicker({
                //minDate: -1, maxDate: "+1D",
                changeMonth: true,
                changeYear: true,
                todayBtn: "linked",
                language: "id",
                autoclose: true,
                todayHighlight: true,
                dateFormat: 'ddMyy'
            });
          });
        }

        $("document").ready(function(){
          readyDatePicker1();
        });
        
        
    </script>
    <script>
        $(document).ready(function() {

            $("div.desc").prop('disabled', false).hide("slow");

            $("input[name$='choice_tgl']").click(function() {
                var test = $(this).val();
                

                if (test === "1") {
                  if ($("form #div_tgl2").length > 0) {
                    $("form #div_tgl2").remove();
                  }
                } else if(test == "2") {
                  if ($("form #div_tgl2").length === 0) {
                    var tgl2 = $("#div_tgl2_proto").clone();
                    tgl2.insertAfter("#div_tgl1");
                    tgl2.attr("id", "div_tgl2");
                    tgl2.find("#tgl2_proto").attr("id","tgl2");                    
                    tgl2.show();
                    readyDatePicker2();
                  }
                }
                /*
                $("div.desc").hide("slow");
                $("#div_tgl" + test).prop('disabled', false).show("slow");
                */
            });
        });
    </script>
    <!-- Inline CSS based on choices in "Settings" tab -->
    <style>
        .bootstrap-iso .formden_header h2,
        .bootstrap-iso .formden_header p,
        .bootstrap-iso form {
            font-family: Arial, Helvetica, sans-serif;
            color: black
        }

        .bootstrap-iso form button,
        .bootstrap-iso form button:hover {
            color: white !important;
        }

        .asteriskField {
            color: red;
        }
    </style>
</head>

<!-- HTML Form (wrapped in a .bootstrap-iso div) -->
<div class="panel panel-primary">
    <div class="panel-heading"><b>MENU BANTU SALES</b></div>
    <div class="panel-body">
        <div class="bootstrap-iso">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-6 col-sm-6 col-xs-12">
                        <form id="form_sales" method="get" action="sales.php">
                            <div class="form-group ">
                                <label class="control-label requiredField" for="masalah">
                                    TOKO
                                    <span class="asteriskField">
        *
       </span>
                                </label>
                                <select class="select form-control" name="act" required>
                                    <option value="">- PILIH TOKO -</option>
                                    <option value="all">#### ALL TOKO ####</option>
                                    <option value="2D01">2D01 - PERUM OMA [PEOM]</option><option value="2D02">2D02 - BOTANIA BATAM [BOTA]</option><option value="2D03">2D03 - PUTRI HIJAU [PUHI]</option><option value="2D04">2D04 - TIBAN [TIBA]</option><option value="2D05">2D05 - PERMATA LAGUNA [PELA]</option><option value="2D06">2D06 - PERUM PEMDA II [PPII]</option><option value="2D07">2D07 - MERAPI SUBUR [MESU]</option><option value="2D08">2D08 - SAGUBA [SGUA]</option><option value="2D09">2D09 - TAMAN CIPTA INDAH [TCPI]</option><option value="2D10">2D10 - MITRA CENTRE [MICE]</option><option value="2D11">2D11 - TIBAN GLOBAL [TGLO]</option><option value="2D12">2D12 - SIMPANG BSI [SPBI]</option><option value="2D13">2D13 - PINKY AVIARI [PIRI]</option><option value="2D14">2D14 - PUTRA JAYA [PURJ]</option><option value="2D15">2D15 - TMN LAGUNA INDAH [TLIH]</option><option value="2D16">2D16 - LEGENDA MALAKA [LEKA]</option><option value="2D17">2D17 - LEGENDA JUNCTION [LEJU]</option><option value="2D18">2D18 - KUDA PUTIH [KUPU]</option><option value="2D19">2D19 - BENGKONG INDAH [BKIH]</option><option value="2D20">2D20 - TIBAN MC DERMOT [TMDE]</option><option value="2D21">2D21 - BUANA INDAH [BUIN]</option><option value="2D22">2D22 - KOMP. GMP [KGMP]</option><option value="2D23">2D23 - BENGKONG PALAPA [BEPA]</option><option value="2D24">2D24 - CENTRAL LEGEND POINT [CLEP]</option><option value="2D25">2D25 - GRIYA FULI [GRFU]</option><option value="2D26">2D26 - DAPUR 12 [DP121]</option><option value="2D27">2D27 - AVIARI 2 [AVI2]</option><option value="2D28">2D28 - MARCHELIA [MCHA]</option><option value="2D29">2D29 - VALLY PARK [VARK]</option><option value="2D30">2D30 - PRIMA GARDEN [PIGA]</option><option value="2D31">2D31 - SIMPANG NATO [SITO]</option><option value="2D32">2D32 - HANG TUAH [HATU]</option><option value="2D33">2D33 - PANTAI GADING [PADI]</option><option value="2D34">2D34 - TAMAN RAYA [TAMA]</option><option value="2D35">2D35 - RKT AVIARI [RKTA]</option><option value="2D36">2D36 - PUTRA JAYA BINTAN [PJAB]</option><option value="2D37">2D37 - SERAYA [SEAY]</option><option value="2D38">2D38 - KINTAMANI [KINI]</option><option value="2D39">2D39 - PHOENIX [PHOX]</option><option value="2D40">2D40 - NASA SENTOSA [NASE]</option><option value="2D41">2D41 - SAN FRANSISCO[SFCO]</option><option value="2D42">2D42 - SUNGAI PANAS [SNAS]</option><option value="2D43">2D43 - AIR MAS [AMAS]</option><option value="2D44">2D44 - TAMAN CARINA [TMCR]</option><option value="2D45">2D45 - MITRA RAYA [MIYA]</option><option value="2D46">2D46 - MARBELLA 2 [MAR2]</option><option value="2D47">2D47 - HANG LEKIR [HALE]</option><option value="2D48">2D48 - BENGKONG RATU [BGRT]</option><option value="2D49">2D49 - ROYAL SINCOM [ROSI]</option><option value="2D50">2D50 - GRAHA TIRTA INDAH [GRTI]</option><option value="2D51">2D51 - MARBELLA [MELA]</option><option value="2D52">2D52 - PURIMAS/F [PUMS]</option><option value="2D53">2D53 - JODOH SQUARE [JOSU]</option><option value="2D54">2D54 - OZON [OZON]</option><option value="2D55">2D55 - GOLDEN PRAWN [GOWN]</option><option value="2D56">2D56 - NEW HOLIDAY [NEHO]</option><option value="2D57">2D57 - BUANA IMPIAN/F [BUIM]</option><option value="2D58">2D58 - PANTAI GADING [PAIG]</option><option value="2D59">2D59 - CITRA NUSA NIAGA [CINA]</option><option value="2D60">2D60 - SURYA MANDIRI [SUMI]</option><option value="2D61">2D61 - PERMATA HIJAU [PEHI]</option><option value="2D62">2D62 - BALOI CENTRE [BACE]</option><option value="2D63">2D63 - BDR HANG NADIM [HGNM]</option><option value="2D64">2D64 - PENUIN CENTRE [PECE]</option><option value="2D65">2D65 - GENTA 1 [GENT]</option><option value="2D66">2D66 - DUTA MAS [DUMA]</option><option value="2D67">2D67 - SAWANG PERMAI[SAPE]</option><option value="2D68">2D68 - VILLA TAMAN BUNGA [VATA]</option><option value="2D69">2D69 - KARTINI [KITI]</option><option value="2D70">2D70 - BUANA VISTA [BVTA]</option><option value="2D71">2D71 - BUNGA RAYA [BAAY]</option><option value="2D72">2D72 - BOTANIA GARDEN [BOGA]</option><option value="2D73">2D73 - MELCEM [MELM]</option><option value="2D74">2D74 - GMP 2 [GMP2]</option><option value="2D75">2D75 - GOLDEN LAND [GOLA]</option><option value="2D76">2D76 - MEGA LEGENDA [MEDA]</option><option value="2D77">2D77 - TUNAS 2 [TUN2]</option><option value="2D78">2D78 - GRIYA KURNIA DJAYA [GIYA]</option><option value="2D79">2D79 - SRIJAYA ABADI [SAAB]</option><option value="2D80">2D80 - BLOK 3 [BLK3]</option><option value="2D81">2D81 - ALIBABA [ALIA]</option><option value="2D82">2D82 - MEDITERANIA [MEIA]</option><option value="2D83">2D83 - SAGULUNG PERMAI [SGPR]</option><option value="2D84">2D84 - CIPTA SARANA [CTRN]</option><option value="2D85">2D85 - SEI HARAPAN [SIHA]</option><option value="2D86">2D86 - NAGOYA NEWTOWN [NWTN]</option><option value="2D87">2D87 - CENDANA [CEND]</option><option value="2D88">2D88 - BUKIT KEMUNING [BKKM]</option><option value="2D89">2D89 - LUCKY PERMAI [LCPM]</option><option value="2D90">2D90 - HARAPAN BISNISCENTER/F [HABC]</option><option value="2D91">2D91 - GAJAH MADA/F [GAAD]</option><option value="2D92">2D92 - SEI PANAS 2 [SIPS]</option><option value="2D93">2D93 - CENTRE PARK [CNPK]</option><option value="2D94">2D94 - HAPPY GARDEN [HPGD]</option><option value="2D95">2D95 - TAMAN CIPTA ASRI [TACA]</option><option value="2D96">2D96 - PERUM TAMAN RAYA III [PETR]</option><option value="2D97">2D97 - TAMAN KOTA MAS [TAKS]</option><option value="2D98">2D98 - GRIYA PRATAMA [GITA]</option><option value="2D99">2D99 - TUNAS REGENCY [TARY]</option><option value="2DA1">2DA1 - KAVLING LAMA [KILA]</option><option value="2DA2">2DA2 - PUSKOPKAR [PUKO]</option><option value="2DA3">2DA3 - BUKIT BERUNTUNG [BIBE]</option><option value="2DA4">2DA4 - PEMDA 1 [PEM1]</option><option value="2DA5">2DA5 - TIBAN CENTRE/F [TBCE]</option><option value="2DA6">2DA6 - TIBAN III [TIAN]</option><option value="2DA7">2DA7 - TIBAN LESTARI [TALE]</option><option value="2DA8">2DA8 - BATAM PARK [BAPA]</option><option value="2DA9">2DA9 - ANGGREK SARI [ANSA]</option><option value="2DB1">2DB1 - AVIARI GRIYA PRATAMA [AGIP]</option><option value="2DB2">2DB2 - SPBU KDA [SPBD]</option><option value="2DB3">2DB3 - DERMAGA SUKAJADI [DESU]</option><option value="2DB4">2DB4 - SEI PANCUR [SIPU]</option><option value="2DB5">2DB5 - NAGOYA PARADISE/F [NAPE]</option><option value="2DB6">2DB6 - PUNGGUR [PUGR]</option><option value="2DB7">2DB7 - TIBAN KOPERASI [TIKO]</option><option value="2DB8">2DB8 - SIMPANG 4 MANGSANG [S4MA]</option><option value="2DB9">2DB9 - KAVLING PELOPOR [KIPR]</option><option value="2DC1">2DC1 - PANDAWA/F [PNAW]</option><option value="2DC2">2DC2 - PONDOK PRATIWI [POPA]</option><option value="2DC3">2DC3 - GREEN LAGUNA [GENA]</option><option value="2DC4">2DC4 - KAPITA PLAZA/F [KAZA]</option><option value="2DC5">2DC5 - PURI AGUNG [PUNG]</option><option value="2DC6">2DC6 - MANDALAY [MALY]</option><option value="2DC7">2DC7 - BIDA AYU [BIYU]</option><option value="2DC8">2DC8 - GLORY HOME [GOHO]</option><option value="2DC9">2DC9 - LAKSAMANA BINTAN [LABI]</option><option value="2DD1">2DD1 - CAHAYA GARDEN [CHGA]</option><option value="2DD2">2DD2 - BENGKONG PERMAI [BEPE]</option><option value="2DD3">2DD3 - PINTU 3 BIDA AYU [P3BA]</option><option value="2DD4">2DD4 - BENGKONG SADAI [BESI]</option><option value="2DD5">2DD5 - RAYA TIBAN/F [RYTI]</option><option value="2DD6">2DD6 - NAGOYA 1[NGY1]</option><option value="2DD7">2DD7 - MELCEM 2 [MEL2]</option><option value="2DD8">2DD8 - AKU TAHU [AKTH]</option><option value="2DD9">2DD9 - CIPTA PURI [CAPI]</option><option value="2DE1">2DE1 - NAGOYA [NAGA]</option><option value="2DE2">2DE2 - HANG KESTURI [HAKE]</option><option value="2DE3">2DE3 - ALADIN [ALAI]</option><option value="2DE4">2DE4 - PUTRI HIJAU 2 [PUH2]</option><option value="2DE5">2DE5 - KAVLING BARU [KVBA]</option><option value="2DE6">2DE6 - KAVLING MANGSANG [KVMG]</option><option value="2DE7">2DE7 - TIBAN IMPIAN [TIPI]</option><option value="2DE8">2DE8 - SPBU NONGSA/F [SNON]</option><option value="2DE9">2DE9 - SP HANG LEKIR 1 [SPHL]</option><option value="2DF1">2DF1 - MEDITERANIA 2 [MED2]</option><option value="2DF2">2DF2 - BENGKONG HARAPAN/F [BEHA]</option><option value="2DF3">2DF3 - TIBAN FORTUNE [TIFO]</option><option value="2DF4">2DF4 - CIPTA MANDIRI [CTMR]</option><option value="2DF5">2DF5 - KOMPLEK MANGSANG [KOMA]</option><option value="2DF6">2DF6 - TANJUNG SENGKUANG [TGSE]</option><option value="2DF7">2DF7 - BUKIT CITRA LESTARI [BUCL]</option><option value="2DF8">2DF8 - PELABUHAN SEKUPANG [PSFT]</option>                                </select>
                            </div>
                            <div>
                                <input type="radio" name="choice_tgl" value="1" /> PER TANGGAL
                            </div>
                            <div>
                                <input type="radio" name="choice_tgl" checked="checked" value="2" /> PER RANGE TANGGAL
                            </div>
                            <div id="div_tgl1" class="form-group ">
                                <label class="control-label requiredField" for="tgl">
                                    TANGGAL
                                    <span class="asteriskField">
        *
       </span>
                                </label>
                                <div class="input-group">
                                    <div class="input-group-addon">
                                        <i class=" glyphicon glyphicon-calendar">
                                        </i>
                                    </div>
                                    <input class="form-control" id="tgl" name="tgl" placeholder="" type="text" readonly required/>
                                </div>
                            </div>


                            








                            <div class="form-group">
                                <div>
                                    <button class="btn btn-primary " name="kirim" value="request" type="submit" onclick="return confirm('Apakah anda yakin untuk mengirimnya?">
                                        Submit
                                    </button>
                                </div>
                            </div>
                        </form>



                        <div id="div_tgl2_proto" class="desc form-group" style="display:none;">
                            <label class="control-label requiredField" for="tgl2">
                                S/D TANGGAL
                                <span class="asteriskField">
    *
   </span>
                            </label>
                            <div class="input-group">
                                <div class="input-group-addon">
                                    <i class=" glyphicon glyphicon-calendar">
                                    </i>
                                </div>
                                <input class="form-control" id="tgl2_proto" name="tgl2" placeholder="" type="text" readonly required />
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-footer"><b>NB : </b>Jika masalah masih berlanjut bisa langsung menghubungi departemen terkait.</div>
</div>

</html>

在重置方法中也在按钮标题中设置nil。 我希望它能解决你的问题

答案 1 :(得分:0)

Sumit给我的答案是解决方案的一部分。谢谢你让我朝着正确的方向前进!使其工作的方法是checkScore()函数需要像这样编写:

    func checkScore(){

    if (topLeftOutlet.currentTitle == topCenterOutlet.currentTitle && topLeftOutlet.currentTitle == topRightOutlet.currentTitle && topLeftOutlet.currentTitle != originalValue && topLeftOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageTopHorizontal

     }

    if (leftCenterOutlet.currentTitle == centerOutlet.currentTitle && centerOutlet.currentTitle == rightCenterOutlet.currentTitle && leftCenterOutlet.currentTitle != originalValue && leftCenterOutlet.currentTitle != nil) {

        announcementArea.text = winnerMessageCenterHorizontal

     }

     if (bottomLeftOutlet.currentTitle == bottomCenterOutlet.currentTitle && bottomLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && bottomLeftOutlet.currentTitle != originalValue && bottomLeftOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageBottomHorizontal
     }

     if (topLeftOutlet.currentTitle == leftCenterOutlet.currentTitle && topLeftOutlet.currentTitle == bottomLeftOutlet.currentTitle && topLeftOutlet != originalValue && topLeftOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageLeftVertical
     }

     if (topCenterOutlet.currentTitle == centerOutlet.currentTitle && topCenterOutlet.currentTitle == bottomCenterOutlet.currentTitle && topCenterOutlet.currentTitle != originalValue && topCenterOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageCenterVertical
     }

     if (topRightOutlet.currentTitle == rightCenterOutlet.currentTitle && topRightOutlet.currentTitle == bottomRightOutlet.currentTitle && topRightOutlet.currentTitle != originalValue && topRightOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageRightVertical
     }

     if (topLeftOutlet.currentTitle == centerOutlet.currentTitle && topLeftOutlet.currentTitle == bottomRightOutlet.currentTitle && topLeftOutlet.currentTitle != originalValue && topLeftOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageDiagonal1
     }

     if (topRightOutlet.currentTitle == centerOutlet.currentTitle && topRightOutlet.currentTitle == bottomLeftOutlet.currentTitle && topRightOutlet.currentTitle != originalValue && topRightOutlet.currentTitle != nil){

        announcementArea.text = winnerMessageDiagonal2
     }
}

每个&#34;如果&#34;语句需要检查按钮标题是否不等于originalValue,它是我创建的空字符串变量。另外,和Sumit提到的一样,它需要检查按钮标题是否等于nil。