我遇到了一个问题。尝试segue(实际上解开一个segue)后,我想以编程方式移动到另一个选项卡(用户从前面的选项卡开始一个动作,被缝到一个模态视图并完成他/她的任务,在完成模态视图后解开,然后我想给用户反馈并在最后一个标签中显示(这样用户可以在此之后开始新的操作或稍后再回到反馈)。
$(document).ready(function(){
$(".template_tax:first").hide(); //hide template
/* Add new item based on hidden template */
$(".add_tax").click(function() {
var newItem = $(".template_tax:first").clone();
newItem.find("select").attr("id", "field_tax" + ($(".template_tax").length + 0)); //rewrite id's to avoid duplicates
newItem.find("input").attr("id", "field_tax" + ($(".template_tax").length + 0)); //rewrite id's to avoid duplicates
newItem.show(); //show clone of template
$(".template_tax:last").after(newItem);
bindRemove();
});
/* Bind remove function to last added button*/
function bindRemove() {
$(".remove_tax:last").click(function(e) {
if ($(".remove_tax").length > 1)
$(this).parents(".template_tax").remove();
});
}
/* Execute bind-function at startup */
bindRemove();
});
当我在同一个viewcontroller中添加一个按钮并执行此操作时,它可以正常工作:
@IBAction func backToSimulatorView(segue:UIStoryboardSegue) {
self.ToFeedback(segue)
}
此代码不起作用:
@IBAction func ToFeedback(sender: AnyObject) {
let tc = self.tabBarController! as UITabBarController
tc.selectedIndex = 2
}
我可以使用通知解决它,但感觉有点像黑客。
答案 0 :(得分:-1)
这是回调机制,在FirstViewController.m
中- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//object ref, we need to set the callback property of destination view controller
SecondViewController *svc = (SecondViewController*)[segue destinationViewController];
[svc setMyCallBack:^(NSString *dat) {
NSLog(@"%@", dat); //Prints the data posted back by Secondviewcontroller.
}];
}
在SecondViewController.h中
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
- (void)setMyCallBack:(MyNewCallBack)cb; //First view controller sets the callback through this method.
@end
在SecondViewController.m中
@interface SecondViewController ()
@property (nonatomic, strong)MyNewCallBack mycb;// we need this property to access the callback where ever we need.
@end
- (IBAction)NavigationBarBackAction:(id)sender {
self.mycb(@"MyData"); // Here i am passing a string to First View Controller.
}
- (void)setMyCallBack:(MyNewCallBack)cb {
self.mycb = cb;
}
我希望它有所帮助