- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个名为 vc0 的 View Controller ,它是这样显示的:
[self presentViewController: vc1 animated: YES completion: nil];
在 vc1 中我有一个按钮来呈现另一个 View Controller :
[self presentViewController: vc2 animated: YES completion: nil];
然后在 vc2 中,我有一个关闭 View Controller 的按钮:
[self dismissViewControllerAnimated:YES completion: ^{
// over here I call one method in vc1
}
正如预期的那样,它返回到 vc1.. 然而,在 vc1 中有一个按钮可以通过关闭 View Controller 返回到 vc0,如下所示:
[self dismissViewControllerAnimated:YES completion:nil];
但由于某种原因它不起作用, View Controller 不会被解散回 vc0。当我第一次展示 vc1 时,我可以按下按钮关闭 View Controller 并且它可以工作。但是当我按下按钮打开 vc2,当我关闭 vc2 回到 vc1,然后我按下按钮关闭 View Controller 时,它就不起作用了。
抱歉,如果问题有点不清楚,我很难表达我想说的话。
还有一件事:
我尝试替换 vc1 中的 dismissViewControllerAnimated:
以手动呈现 vc0,但随后我在控制台中收到一条日志,指出我正在尝试呈现 vc0,但 vc1 的 View 不在窗口层次结构中。这是什么意思?
感谢您的帮助!
更新:
在这种情况下,VC0 是 MenuMileIndexViewController
- VC1 是 FlightViewController
- VC2 是 BookmarksTableViewController
这里是涉及的代码:
MenuMileIndexViewController:
- (IBAction)goToOriginPage {
FlightRecorder *origin = [[FlightRecorder alloc] init];
[self presentViewController:origin animated:YES completion:nil];
}
Flight Recorder:
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
[self bringUpBookmarkkTable];
}
- (void) bringUpBookmarkkTable {
BookmarkTableViewController *bookmarkTVC = [[BookmarkTableViewController alloc] init];
[bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[self presentViewController:bookmarkTVC animated:YES completion:nil];
}
- (IBAction)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict {
presetBookmarkContext = [dict mutableCopy];
bookmarkMode = YES;
NSString *compiledText = nil;
NSNumber *number1 = [NSNumber numberWithInt: 1];
if ([dict objectForKey: @"bookmarkTag"] == number1) {
compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"origin"], [dict objectForKey: @"destination"]];
}
else {
compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"destination"], [dict objectForKey: @"origin"]];
}
compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Origin: " withString:@""];
compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Destination: " withString:@""];
flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil];
NSString *string = [NSString stringWithFormat: @"\nMiles: %.2f\nFlight: %@\nDate: %@", [[dict objectForKey: @"miles"] floatValue], compiledText, [[NSUserDefaults standardUserDefaults] objectForKey:@"tempD"]];
UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
[bvkBookmarkAlertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[TheMileIndexViewController addDesiredMilesToIndex: [[flightContext objectForKey: @"miles"] doubleValue]];
[TravelLogViewController addFlight: flightContext];
if (!bookmarkMode) {
if ([checkbox isSelected]) {
[BookmarkHandler uploadBookmark: bookmarkFlightContext];
}
}
}
if (buttonIndex == 0) {
if ([alertView.title isEqualToString: @"Confirmation"]) {
bookmarkMode = NO;
}
}
}
BookmarksTableViewController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated: YES];
NSDictionary *dict = [[BookmarkHandler bookmarkCollection] objectAtIndex: indexPath.row];
fl = [[FlightRecorder alloc] init];
[self dismissViewControllerAnimated:YES completion:^{
[fl endBookmarkProcessWithBookmarkCollection: dict];
}];
}
现在,我已经在模拟器中创建了应用程序的屏幕录像,显示出了什么问题。我可以通过电子邮件将其发送给您以供引用。这样我就可以通过电子邮件将其发送给您。
最佳答案
我建议始终从实际呈现它的 VC 中解雇 VC - 使用委托(delegate)。这实际上也是 Apple 推荐的方式 - 正如我之前对有关此问题的问题的回答所指出的那样。
因此,如果您有 VC0 呈现 VC1,则在 VC0 中也有关闭 VC1 代码,使用委托(delegate)方案。
我了解到这是处理呈现和解散的最省事的方法 - 尽管有时它可以在 VC1 本身中解散 VC1。
我问了一个非常相关的问题,您可能也有兴趣检查一下。它显示 code...
ps 我还读到有些人只拒绝 VC1 - 这反过来也会拒绝 VC2。但是,如果我之前的建议有效,我不会这样做。有时我在执行过程中得到信息(没有崩溃),VC 不再存在或与之相关的任何东西 - 所以我认为这不是最好的解决方案。但如果我之前的建议不起作用,你可以试试第二个。
虽然不能保证这会持续到新 iOS 的更新,因为这个问题现在一直困扰着我几个 iOS 更新 :-),所以我决定采用标准的推荐路线。
编辑:这是我修改后的代码 - 它可以正常工作 - 但是不清楚你打算在用户响应警报后发生什么以及警报应该在 VC1 还是 VC0 上。无论如何使用委托(delegate)和回调我没有看到任何问题。请解释我是否错过了你的观点......
FlightViewControllerProtocol.h
@protocol FlightViewControllerProtocol <NSObject>
-(void) dismissVCAndEndBookmark;
@end
FlightViewController.m
#import "FlightViewController.h"
#import "FlightViewControllerProtocol.h"
#import "BookmarksTableViewController.h"
@interface FlightViewController ()
@end
@implementation FlightViewController
@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
[self bringUpBookmarkkTable];
}
- (IBAction) bringUpBookmarkkTable {
BookmarksTableViewController *bookmarkTVC = [[BookmarksTableViewController alloc] init];
bookmarkTVC.delegate = self;
[bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[self presentViewController:bookmarkTVC animated:YES completion:nil];
}
- (IBAction)cancel {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict {
// presetBookmarkContext = [dict mutableCopy];
// bookmarkMode = YES;
NSString *compiledText = nil;
NSNumber *number1 = [NSNumber numberWithInt: 1];
if ([dict objectForKey: @"bookmarkTag"] == number1) {
compiledText = @"Text1";
}
else {
compiledText = @"Text2";
}
// flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil];
NSString *string = compiledText;
UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil];
[bvkBookmarkAlertView show];
}
- (void) dismissVCAndEndBookmark {
[self dismissViewControllerAnimated:YES completion:nil];
[self endBookmarkProcessWithBookmarkCollection: nil];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSLog(@"alertView1");
}
if (buttonIndex == 0) {
NSLog(@"alertView2");
}
}
@end
书签TableViewController.h
@interface BookmarksTableViewController : UIViewController
{
id delegate;
}
@property (nonatomic,strong) id delegate;
@end
BookmarksTableViewController.m
- (IBAction)goBack {
[self.delegate dismissVCAndEndBookmark];
}
如果我正确理解您的意图,Esp BookmarksTableViewController.m 中的回调似乎是您实现中的主要问题。
关于iphone - 关闭 ViewController : not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12649834/
我现在在另一个 ViewController 之上展示了一个几乎透明的 ViewController,但它禁用了与下面的 ViewController 的交互。这是我目前使用的代码: topContr
我有一个 View Controller ,其工具栏上有 3 个 UIButtons,可以打开一个新的 View Controller 作为弹出窗口。我在 Storyboard 中创建了 Segues
这个问题在这里已经有了答案: Delegates in swift? (15 个答案) 关闭 3 年前。 在我的场景中,我试图在关闭 View Controller 期间将值从 ViewContro
假设我有两个 viewController,AViewController 和 BViewController。 AViewController 嵌入在 NavigationController 中(
我有 3 个 View Controller A、B 和 C。 我在 vcA 上,我使用 推送 vcB [self.navigationController pushViewController:vc
我想知道如何在 VC3 和 VC1 之间传递数据。 我拥有的是 Root View (VC1)。然后你输入一个tableView,如果你选择一个项目,你将进入tableView2(VC3) - 类别
我在使用 Unwind Segue 时遇到问题。我有 3 个 ViewController,它们都在名为 ViewController.swift 的类下。我试图获取最后一个 ViewControll
我有一个应用程序,其左上角有一个汉堡菜单。按下时,菜单会以 90% 的不透明度淡入当前 ViewController,显示应用程序的主要导航选项。 我使用下面的代码从页面呈现 viewControll
我的问题是,当我呈现一个 UIViewController 时,呈现的 View 变黑了。 我有一个名为 mainViewController 的 UIViewController,它是我窗口的 Ro
例如,当我呈现UIActivityViewController或UIAlertController时,我仍然可以看到背景viewController,我不想在viewContollerA中嵌入view
我从second viewController进入了third viewController,但是我不想回到第二个viewController,我需要直接回到我的第一个viewController,我
我的应用程序是标签栏(+ 导航)应用程序。在 FirstViewController 中,我调用 onModalView。 -(void) onFilter { FilterViewControl
Photo Sharing 部分是我写的一个ViewController,当我按下正确的项目时,PhotoSharingViewController 会动画出现。 这是我的代码: PhotoShari
我正在尝试在另一个 View Controller 中使用 subview Controller 的应用程序。我有一个 VC,我正在实例化另一个 VC,并在外部 VC 内使用它自己的 xib。 我使用
我正在寻找有关包含 ViewController 的滚动的想法。我需要知道包含 View Controller 的位置。我已经尝试过 UIPageViewController 但我无法让滚动委托(de
我有一个带有嵌入式 NavigationController 的基本 ViewController 来提供工具栏。 ToolBar 有三个按钮,用于通过 segue 调用 ViewController
我的应用程序中的伙计们,我在应用程序委托(delegate)方法中有一些代码 application:didFinishLaunchingWithOptions:确定初始 View Controlle
我遇到了一个问题,我将对此进行描述,并且发现了一些类似的问题,但我认为与我的问题无关。 我有一个 UITabBarController,其中有 2 个用于 VC1 和 VC2 的选项卡。 VC1 转到
我的应用程序中有一个计时器。如果计时器达到 0,则用户将被带到失败页面。当这种情况发生时,之前的 ViewController 没有被关闭,导致更多的内存被占用。 如果出现失败页面,我希望关闭之前的
在问这个问题之前,我当然做了一些调查。我在 google 和 stackoverflow 上找到的链接或主题之一是: How do I load another view controller fro
我是一名优秀的程序员,十分优秀!