我已经阅读了Staked Overflow上的很多内容并观看了关于使用代码生成按钮的YouTube视频,但我无法弄清楚如何根据按钮创建if语句。这嵌套在另一个语句中,因为当用oringal yes或no按钮回答一个问题时,我会问另一个问题。
我有:
var btn = UIButton(frame: CGRectMake(50, 50, 150, 20));
btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
btn.setTitle("My Button Text!!", forState:UIControlState.Normal);
btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn);
func buttonTapped(sender: UIButton!) {
println("Button Tapped!!!")
}
但不是打印“Button Tapped !!!”我收到“SIGABART”错误。我怎么能使用if语句而不是函数1?有没有更好的方法来怀疑这个函数声明?
完整的代码是:
//
// ViewController.swift
// Living Vermont
//
// Created by Matthew Furtsch on 6/8/15.
// Copyright (c) 2015 Matthew Furtsch. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label1 = UILabel()
label1.text = "Is it a plant?"
label1.font = UIFont.systemFontOfSize(36)
label1.sizeToFit()
//Label location quardnets
label1.center = CGPoint(x: 100, y: 0)
view.addSubview(label1)
UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0.0, options: nil, animations: {
self.label1.center = CGPoint(x: 100, y: 0 + 40)
}, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func journal(sender: AnyObject)
{
}
@IBAction func noButton(sender: AnyObject)
{
label1.text = "no"
var btn = UIButton(frame: CGRectMake(50, 50, 150, 20));
btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
btn.setTitle("My Button Text!!", forState:UIControlState.Normal);
btn.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn);
func buttonTapped(sender: UIButton!) {
println("Button Tapped!!!")
}
}
@IBAction func yesButton(sender: AnyObject)
{
label1.text = "yes"
}
}
答案 0 :(得分:0)
我的猜测是你的buttonTapped函数嵌套在另一个方法中,比如viewDidLoad。它必须是视图控制器类的顶级方法。移动你的buttonTapped方法,使它在你的类中,但不在你的类中的任何方法内。
您应该编辑您的问题并包含您获得的整个崩溃消息以及堆栈跟踪。这可能有助于我们说明出了什么问题。