2017-01-06 9 views
0

これは、整数が一定量以下の場合、ビューコントローラのボタンを無効にするコードです。私は整数のエラー

plancnt.set(20, forKey: "plancnt") 
    plancnt.synchronize() 

を書くとき

if (plancnt.value(forKey: "plancnt") != nil){ 
     plancnt.set(1, forKey: "plancnt") 
    } 
if (plancnt.integer(forKey: "plancnt") <= 1) { 
     two.isEnabled = false 
     three.isEnabled = false 
     four.isEnabled = false 
     five.isEnabled = false 
     six.isEnabled = false 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 2) { 
     three.isEnabled = false 
     four.isEnabled = false 
     five.isEnabled = false 
     six.isEnabled = false 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 3) { 
     four.isEnabled = false 
     five.isEnabled = false 
     six.isEnabled = false 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 4) { 
     five.isEnabled = false 
     six.isEnabled = false 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 5) { 
     six.isEnabled = false 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 6) { 
     seven.isEnabled = false 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 7) { 
     eight.isEnabled = false 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 8) { 
     nine.isEnabled = false 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 9) { 
     ten.isEnabled = false 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 10) { 
     elleven.isEnabled = false 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 11) { 
     twelve.isEnabled = false 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 12) { 
     thirteen.isEnabled = false 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 13) { 
     fourteen.isEnabled = false 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 14) { 
     fifteen.isEnabled = false 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 15) { 
     sixteen.isEnabled = false 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 16) { 
     seventeen.isEnabled = false 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 17) { 
     eighteen.isEnabled = false 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 18) { 
     nineteen.isEnabled = false 
     twenty.isEnabled = false 
    } 
    if (plancnt.integer(forKey: "plancnt") <= 19) { 
     twenty.isEnabled = false 
    } 

ただし、ボタンの1を除いてボタンのすべてが無効になっています。私の偉大な/より小さい兆候が間違っているか何か他のものがあれば、私は確信していません。

+7

の下にこれを入力します!あなたは配列について聞いたことがありますか? –

+0

親愛なる神。確かに...何か – Alexander

+0

@OrenEdrich配列を使う! Swift言語ガイドを読んでください。質問がある場合は、ここに戻って助けてください:) – Alexander

答えて

0

を「有効」ボタンをunclick、コードは恐ろしいであることのviewDidLoad

if (plancnt.integer(forKey: "plancnt") >= 1) { 
     one.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 2) { 
     two.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 3) { 
     three.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 4) { 
     four.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 5) { 
     five.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 6) { 
     six.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 7) { 
     seven.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 8) { 
     eight.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 9) { 
     nine.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 10) { 
     ten.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 11) { 
     elleven.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 12) { 
     twelve.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 13) { 
     thirteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 14) { 
     fourteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 15) { 
     fifteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 16) { 
     sixteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 17) { 
     seventeen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 18) { 
     eighteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 19) { 
     nineteen.isEnabled = true 
    } 
    if (plancnt.integer(forKey: "plancnt") >= 20) { 
     twenty.isEnabled = true 
    } 
2

ボタンを無効にしただけなので、plancntが変更されたときには再び有効にすることはできません。そしてあなたのコードは私に頭痛を与えます。このような何かを試してみてください:

if plancnt.value(forKey: "plancnt") == nil { 
    plancnt.set(1, forKey: "plancnt") 
} 

let buttons = [one, two, three, ..., twenty] 
let cnt = plancnt.integer(forKey: "plancnt") 

for i in 0..<buttons.count { 
    buttons[i].isEnabled = i < cnt 
} 
+0

これはうまくいかない、20個のアイテムの配列が原因でXcodeがクラッシュする可能性がある。私はこれを自分自身で試してみるとインデックスに登録できませんでした。 http://stackoverflow.com/questions/13831559/xcode-stuck-on-indexing –

+0

1) 'Cmd + Shift + K'を押してプロジェクトをきれいにして再構築する。2)それでも動作しない場合は、Xcodeにヒントを与える'let buttons:[UIButton] = [one、two、...]' –

+0

私はプロジェクトをクリーンアップしようとしましたが、プロジェクトをビルドしようとしましたが、どちらも終了できません。 –

0

このような何か:右舷ファイルで

var buttons = [one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen,sixteen,seventeen,eighteen,nineteen,twenty] 
     let i: Int = plancnt.value(forKey: "plancnt") 
     for index in i..<buttons.count { 
      print("\(index)") 
      buttons[index].isEnabled = false 
     } 
+0

これはうまくいかず、20個のアイテム配列が原因でXcodeがクラッシュする可能性があります。私はこれを自分自身で試してみるとインデックスに登録できませんでした。 stackoverflow.com/questions/13831559/xcode-stuck-on-indexing –

関連する問題