【问题标题】:Objective-c App crashing in iOS 13(Search bar issue)Objective-c App 在 iOS 13 中崩溃(搜索栏问题)
【发布时间】:2019-10-04 20:55:03
【问题描述】:

请有人帮助我,我不熟悉 Objective-c。应用程序在 iOS 13 中崩溃 在控制台发现如下错误

* 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“搜索栏布局的视图丢失或分离。应用程序不得从层次结构中删除 >。'

搜索条码

 UIImageView *searchBackView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 45, 320, 30)];
    searchBackView.image = [UIImage imageNamed:@"search1.gif"];
    searchBackView.userInteractionEnabled=YES;
    searchBackView.backgroundColor=[UIColor clearColor];
    [tableHeader addSubview:searchBackView];

  searchBar=[[[UISearchBar alloc] init] autorelease];
    searchBar.delegate=self;
    [searchBar setKeyboardType:UIKeyboardTypeDefault];
    searchBar.barStyle = UISearchBarIconClear;
    searchBar.frame=CGRectMake(17, 3, 285, 30);
    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search1.gif"] forState:UIControlStateNormal];
    searchBar.backgroundColor=[UIColor clearColor];
    [searchBackView addSubview:searchBar];
    [searchBackView release];
    [tableHeader addSubview:label];

        for (UIView *subView in self.searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subView.subviews){
        if ([secondLevelSubview isKindOfClass:[UIImageView class]]){
            [secondLevelSubview removeFromSuperview];
        }
            }
        }



    searchField = nil;
        for (UIView *subView in self.searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subView.subviews){
                if ([secondLevelSubview isKindOfClass:[UITextField class]])
                {
                    searchField = (UITextField *)secondLevelSubview;
                    break;
                }
            }
        }

        for (UIView *subview in searchBar.subviews)
        {
            for (UIView *secondLevelSubview in subview.subviews){
                if ([secondLevelSubview conformsToProtocol:@protocol(UITextInputTraits)])
                {
                    [(UITextField *)secondLevelSubview setClearButtonMode:UITextFieldViewModeNever];
                }
            }
        }
 if (searchField) {
    searchField.leftViewMode = UITextFieldViewModeNever;
    }
    BluemenAppDelegate *delegate=(BluemenAppDelegate*)[[UIApplication sharedApplication] delegate];
    if (isShowlist==YES) {
        delegate.search_string = @"";
        searchBar.text = delegate.search_string;
        isShowlist = NO;
    }

        searchBar.text = delegate.search_string;

更新

现在我可以启动应用程序,但现在如果单击应用程序中的任何文本字段,它会崩溃并给出 XPC 连接中断 错误

【问题讨论】:

  • 你能不能把searchBar=[[[UISearchBar alloc] init] autorelease];改成searchBar=[[UISearchBar alloc] init];并删除[searchBackView release];行试试?
  • 试过但没有运气 :-( 如果我评论 [secondLevelSubview removeFromSuperview]; 行,那么应用程序不会崩溃,但如果我这样做,搜索栏会出现问题
  • 只是为了让我能理解你想要达到的目标,你想要一个清晰的搜索栏吗?如果您不删除提到的视图,您能否发布它的外观以及外观的屏幕截图?

标签: ios objective-c ios13


【解决方案1】:

如果我理解正确,您正在尝试实现具有清晰背景的 SearchBar。如错误消息所示,iOS 13 不允许删除 UISearchBarBackground。您可以尝试一种解决方法:

这将使搜索背景图像透明,而不会将其删除。

for (UIView *subView in self.searchBar.subviews) {
    for (UIView *secondLevelSubview in subView.subviews){
        if ([secondLevelSubview isKindOfClass: [UIImageView class]]) {
            secondLevelSubview.alpha = 0.0;
            break;
        }
    }
}

只需将这部分代码替换为这段代码,然后检查它是否符合您的预期。

【讨论】:

  • @summerfin3 感谢您的回复。当我尝试用您共享的代码替换时,我能够启动应用程序而不会发生任何崩溃,但是当我单击搜索栏时它崩溃并在控制台中给出 XPC 连接中断错误
  • 嗯,代码可能会陷入其中一个循环。尝试添加中断;在每个 for 语句的说明之后。
  • 即使在循环中添加中断后也会出现同样的问题?。我猜测 iOS 13 中的搜索栏文本字段可能存在一些问题。
  • 可能,iOS 13 上的 SearchBar 发生了很大变化。我做了一个关于如何实现它的 sn-p,也许它对你有用pastebin.com/ByP738bq
  • @summerfin3 感谢您的努力。我已经实现了您的代码,但没有任何变化,当我单击文本字段时出现相同的错误。但是我注意到这里的问题不仅是搜索栏文本字段,以前我只是因为应用程序已经登录而单击搜索栏文本字段,现在我注销并单击其中一个文本字段登录,现在在这里,我也遇到了同样的 XPC 连接中断 错误。
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2013-02-04
  • 1970-01-01
相关资源
最近更新 更多