我正在学习如何编程并在Xcode中使用Swift项目。主故事板有两个视图控制器。第一个视图控制器简称为ViewController,第二个视图控制器称为HelpScreenViewController。
在ViewController中,我有一个“帮助”按钮,可以将用户切换到HelpScreenViewController。此按钮使用名为“goToHelpScreenSegue”的segue。
在HelpScreenViewController中,我有三个按钮:
我要做的是......
...获取HelpScreenViewController上的“重置游戏”按钮,通过调用在第一个视图控制器中编码的函数来重置游戏。*
为了尝试按照我想要的方式工作,我使用了以下代码:
WITHIN第一个主ViewController
return
依旧......
WITHIN第二个HelpScreenViewController
#include <iostream>
using namespace std;
void CalcInterest(int i);
int main()
{
cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;
for (int account = 0; account < 8; account++) {
CalcInterest(account);
}
system("pause");
return 0;
};
void CalcInterest(int i) {
static const float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
static const int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
static const int AccountNumbers[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
int interest = 0;
if (AccountNumbers[i] == 1001 || AccountNumbers[i] == 4382 || AccountNumbers[i] == 3020 || AccountNumbers[i] == 6245)
interest = (Balance[i] * 0.06);
else
interest = (Balance[i] * 0.03);
cout << AccountNumbers[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}
现在,在上面的代码中,如果用户点击第一个主ViewController中的“帮助”按钮(即goToHelpScreenSegue),它不仅会将用户带到帮助屏幕,而且还会当用户点击“重置游戏”按钮时,调用我想激活的功能。也就是说,目前,它是“帮助”按钮,在将用户带到帮助屏幕之前重置游戏。
现在,在帮助屏幕中,前两个按钮正常工作(但它们不使用segue)。点击第三个按钮(重置游戏一个)只会将用户返回到主屏幕。它不会调用该函数,也不会重置游戏。
我已经失去了我改变代码的次数,试图让它正常工作,但我显然错过了一些非常明显的事情。
特别是,我尝试在主ViewController中使用以下代码:
import UIKit
import iAd
import AdSupport
import AVFoundation //audio
import GameplayKit
class ViewController: UIViewController, ADBannerViewDelegate, MyResetGameProtocol {
@IBOutlet weak var Banner: ADBannerView!
@IBOutlet weak var buttonA: UIButton!
@IBOutlet weak var buttonB: UIButton!
@IBOutlet weak var buttonC: UIButton!
@IBOutlet weak var buttonD: UIButton!
@IBOutlet weak var labelQuestion: UILabel!
@IBOutlet weak var labelScore: UILabel!
@IBOutlet weak var labelTotalQuestionsAsked: UILabel!
@IBOutlet weak var labelFeedback: UILabel!
@IBOutlet weak var buttonNext: UIButton!
var score :Int! = 0
var totalquestionsasked :Int! = 0
var allEntries : NSArray!
var shuffledQuestions: [AnyObject]!
var nextQuestion = -1
var currentCorrectAnswerIndex : Int = 0
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.Banner?.delegate = self
self.Banner?.hidden = true
LoadAllQuestionsAndAnswers()
if #available(iOS 9.0, *) {
shuffledQuestions = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(allEntries as [AnyObject])
nextQuestion++
LoadQuestion(nextQuestion)
// Fallback on earlier versions
}else{
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
LoadQuestionPreiOS9(randomNumber)
}
LoadScore()
AdjustInterface()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let viewController = segue.destinationViewController as! HelpScreenViewController
viewController.controller = self
}
func ResetGame() {
PlaySoundReset()
score = 0
totalquestionsasked = 0
SaveScore()
LoadScore()
}
func PlaySoundReset()
{
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("pcbeep", ofType: "wav")!)
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
} catch {
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func SaveScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(score, forKey: "Score")
defaults.setInteger(totalquestionsasked, forKey: "Out of")
}
func LoadScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
score = defaults.integerForKey("Score")
totalquestionsasked = defaults.integerForKey("Out of")
labelScore.text = "Score: \(score)"
labelTotalQuestionsAsked.text = "out of \(totalquestionsasked)"
}
然而,这导致什么都没发生。我的意思是主屏幕上的按钮正常工作(将用户带到帮助屏幕,不错误地调用resetGame功能)。并且,在帮助屏幕中,前两个按钮按预期工作,但“重置游戏”按钮只是将用户返回到第一个屏幕,但没有调用ResetGame功能。
我还尝试从我的代码和连接检查器中删除“重置游戏”按钮的IBActions,但这也没有任何区别。
任何帮助都会受到最高的赞赏,因为我没有得到它! :(
答案 0 :(得分:0)
我同意MikeG的意见,您应该了解代理人应该如何实施。但是你在这段代码中做错的事情是你实际上没有在你的委托上调用ResetGame()函数。尝试以这种方式实现您的@IBAction函数:
@IBAction func buttonResetGame(sender: AnyObject) {
controller?.ResetGame()
}
是的,如果我理解你的逻辑,你的HelpScreenViewController不应该实现MyResetGameProtocol,因为你的ViewController是实现它的人。