私は3つのView Controller(VC)を持っています。私の2番目のVCにはスタートボタンと3番目のVCがあります。私は3番目のVCに移動すると時間が実行されるはずです。これを行う方法? クイズアプリの場合前のView Controllerを参照するには?
-6
A
答えて
1
あなたは簡単にこれを行うことができます:
使用してストーリーボードUIを作成します。
コードは以下の通りです:
ViewController2.m:
#import "ViewController2.h"
@interface ViewController2()
{
NSTimer *timer;
int time_count;
}
@property (weak, nonatomic) IBOutlet UILabel *time_label;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(handleTimer:)
userInfo:nil
repeats:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)handleTimer:(id)sender {
_time_label.text = [NSString stringWithFormat:@"%d", time_count++];
}
@end
結果次のようなものです:
スウィフト3
メインコード:
import UIKit
class ViewController2: UIViewController {
@IBOutlet weak var time_label: UILabel!
var time_count = 0
override func viewDidLoad() {
super.viewDidLoad()
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerHandler), userInfo: nil, repeats: true)
}
func timerHandler() {
self.time_count += 1
self.time_label.text = "\(self.time_count)"
}
}
関連する問題
- 1. View Controllerの終了は直前のView Controllerに戻りません
- 2. UITabBarControllerの前にView Controllerを起動するには?
- 3. 別のView ControllerでView Controllerを起動する方法は?
- 4. View Controller NIBを別のView Controllerに追加する
- 5. コンテナビュー内の別のView ControllerからView Controllerをロードする
- 6. ViewDidAppearの前にiPhoneをポップするView Controller
- 7. .swiftを変更してView ControllerではなくTable View Controllerに接続する
- 8. デザイン時にView ControllerオブジェクトをTable View Controllerに簡単に変換する(ストーリーボード)
- 9. 別のView Controllerに戻って別のView Controllerに転送する方法
- 10. ユーザーが別のView Controllerにログインした後、Main View Controllerをリフレッシュする
- 11. MylynのJenkins-Viewからファイルを参照するには?
- 12. View ControllerからView ControllerにFirebaseデータを送信
- 13. View Controllerをサブクラス化する
- 14. View Controller前のビューを閉じて再表示するコントローラ
- 15. View Controllerを閉じるには
- 16. Table View Controllerのタイプを通常のView Controllerに変更するにはどうすればよいですか?
- 17. ファイルApp View Controller?
- 18. view to controller communication
- 19. View Controller resize
- 20. iOS5 Second View Controller
- 21. View Controller lifecycle
- 22. iPhone View Controller Register
- 23. AngularJS view-controller
- 24. iOSのView Controllerで複数のView Controllerを表示する方法
- 25. 最後のView Controllerの3つのView Controllerの近接度
- 26. View Controllerをナビゲーションコントローラにプッシュ
- 27. View Controllerから別のView Controllerに時間を転送するにはどうすればいいですか?
- 28. Table View Controllerを自分のView Controllerサブクラスに設定するにはどうすればよいですか?
- 29. View Controllerの配列内のView Controllerを解放するにはどうすればいいですか?
- 30. UICollectionViewセルからTab Bar Controllerを参照する方法
このhttp://stackoverflow.com/questions/27894834/swift-how参照-to-use-nstimer-background –