显示弹出框使我的 iOS 8 应用程序崩溃

Posted

技术标签:

【中文标题】显示弹出框使我的 iOS 8 应用程序崩溃【英文标题】:Displaying a popover crashes my iOS 8 app 【发布时间】:2014-10-06 20:07:59 【问题描述】:

从多个不同的视图控制器调用的单个弹出框没有问题,除了一个特定的视图控制器。所有视图控制器都来自同一个父类,因此使用相同的函数来显示弹出框。该函数使用presentPopoverFromRect来显示有问题的弹出框。在问题视图控制器上,我可以看到弹出窗口的viewDidLoadfire,然后应用程序因无效参数异常而崩溃。我不知道 UILabel 它在谈论什么,也不知道它为什么要获得它的长度。

-(IBAction) showNormSelector:(id)sender

    if (self.normSelectionPopover == nil)
    
        Ace_Metrix_iPad_2AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"AceMetrixMOBILEHD_Storyboard" bundle:[NSBundle mainBundle]];
        NSMutableDictionary* mdic = [[NSMutableDictionary alloc]initWithCapacity:4];

        mdic[NSLocalizedStringFromTable(@"Industry", appDelegate.stringTableName,@"")] = @(IndustryType);
        mdic[NSLocalizedStringFromTable(@"Category", appDelegate.stringTableName,@"")] = @(CategoryType);
        mdic[NSLocalizedStringFromTable(@"Subcategory", appDelegate.stringTableName,@"")] = @(SubCategoryType);
        mdic[NSLocalizedStringFromTable(@"Brand", appDelegate.stringTableName,@"")] = @(BrandType);

        NSArray* arr = @[NSLocalizedStringFromTable(@"Industry", appDelegate.stringTableName,@""),
                         NSLocalizedStringFromTable(@"Category", appDelegate.stringTableName,@""),
                         NSLocalizedStringFromTable(@"Subcategory", appDelegate.stringTableName,@""),
                         NSLocalizedStringFromTable(@"Brand", appDelegate.stringTableName,@"")];

        self.popOver = (SelectorPopover*)[storyBoard instantiateViewControllerWithIdentifier:@"NormTypeSelector"];
        [self.popOver setSelectionText:mdic];
        [self.popOver setAllKeys:[NSMutableArray arrayWithArray:arr]];
        [self.popOver setDelegate:self];

        self.normSelectionPopover = [[UIPopoverController alloc] initWithContentViewController:self.popOver];
    

    [self.popOver setCurrentSel:@(self.normType)];
    [self.popOver.tableView reloadData];

    [self.normSelectionPopover presentPopoverFromRect:self.normButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp  animated:YES];

SelectorPopover.m

@implementation SelectorPopover

@synthesize delegate;
@synthesize selectionText;
@synthesize allKeys;
@synthesize tag;
@synthesize currentSel;

-(void) viewDidLoad 
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = NO;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7)  //  before ios 7
    
        self.contentSizeForViewInPopover = CGSizeMake(135, [self.tableView rowHeight] * 4);  // Depricated in iOS 7
    
    else
    
        self.preferredContentSize = CGSizeMake(135, [self.tableView rowHeight] * 4);
    

    NSUInteger newIndex[] = 0, 0;
    NSIndexPath* indexPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
    [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];



/*
 -(void) viewWillAppear:(BOOL)animated 
 [super viewWillAppear:animated];
 
 */
/*
 -(void) viewDidAppear:(BOOL)animated 
 [super viewDidAppear:animated];
 
 */
/*
 -(void) viewWillDisappear:(BOOL)animated 
 [super viewWillDisappear:animated];
 
 */
/*
 -(void) viewDidDisappear:(BOOL)animated 
 [super viewDidDisappear:animated];
 
 */


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  // Deprecated in iOS6

    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||  interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;

    return NO;


- (NSUInteger)supportedInterfaceOrientations

    return UIInterfaceOrientationMaskLandscape;


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    // Return the number of sections.
    return 1;



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    // Return the number of rows in the section.
    return [self.selectionText count];



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    static NSString *CellIdentifier = @"SelectCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSString* method = self.allKeys[indexPath.row];
    cell.textLabel.text = method;
    [cell.textLabel setTextColor:AMColorNeutralMidGrey38];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.accessoryView = nil;

    NSNumber* uuid = (self.selectionText)[method];

    if ([self.currentSel integerValue] == [uuid integerValue])
    
        UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark_blue.png"]];
        cell.accessoryView = checkmark;

        [cell.textLabel setTextColor:AMColorTradeMarkLightBlue];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    

    return cell;



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
 // Return NO if you do not want the specified item to be editable.
 return YES;
 
 */


/*
 // Override to support editing the table view.
 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

 if (editingStyle == UITableViewCellEditingStyleDelete) 
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 
 else if (editingStyle == UITableViewCellEditingStyleInsert) 
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 
 
 */


/*
 // Override to support rearranging the table view.
 -(void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
 
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 
 */


#pragma mark -
#pragma mark Table view delegate

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    if (self.delegate)
    
        NSString* key = self.allKeys[indexPath.row];

        NSNumber* value = (NSNumber*) (self.selectionText)[key];

        [self.delegate itemSelected:[value integerValue]];
    



#pragma mark -
#pragma mark Memory management

-(void) didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.


-(void) viewDidUnload 
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        [super viewDidUnload];



-(void) dealloc 
    self.delegate = nil;


@end

SelectorPopover.h

@protocol ItemSelectdDelegate

-(void)itemSelected:(HierarchyType) item;

@end

@interface SelectorPopover : UITableViewController 

@property (nonatomic, weak) id<ItemSelectdDelegate> delegate;
@property (nonatomic,strong) NSMutableDictionary* selectionText;
@property (nonatomic,strong) NSMutableArray* allKeys;
@property (nonatomic,strong) NSNumber* currentSel;

@property (nonatomic) NSInteger tag;

@end

调用栈:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel length]: unrecognized selector sent to instance 0x7b60bd30'
*** First throw call stack:
(
    0   CoreFoundation                      0x03b49df6 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x02f52a97 objc_exception_throw + 44
    2   CoreFoundation                      0x03b51a75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
    3   CoreFoundation                      0x03a9a9c7 ___forwarding___ + 1047
    4   CoreFoundation                      0x03a9a58e _CF_forwarding_prep_0 + 14
    5   CoreFoundation                      0x03a33306 CFStringAppend + 374
    6   CoreFoundation                      0x03a30e2a __CFStringAppendFormatCore + 11754
    7   CoreFoundation                      0x03b26aa5 _CFStringCreateWithFormatAndArgumentsAux2 + 245
    8   Foundation                          0x004e9377 -[NSPlaceholderString initWithFormat:locale:arguments:] + 159
    9   Foundation                          0x004ecc22 +[NSString stringWithFormat:] + 89
    10  UIKit                               0x00aaa7a4 -[UIViewController _presentViewController:withAnimationController:completion:] + 2825
    11  UIKit                               0x00aad032 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
    12  UIKit                               0x00aace84 -[UIViewController presentViewController:animated:completion:] + 224
    13  UIKit                               0x01019011 -[UIPopoverController _presentShimmedPopoverFromRect:inView:permittedArrowDirections:animated:] + 217
    14  UIKit                               0x01019211 -[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:] + 355
    15  Ace Metrix MOBILE HD                0x00111242 -[AdDetailViewController showNormSelector:] + 3762
    16  libobjc.A.dylib                     0x02f687cd -[NSObject performSelector:withObject:withObject:] + 84
    17  UIKit                               0x0094779d -[UIApplication sendAction:to:from:forEvent:] + 99
    18  UIKit                               0x0094772f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    19  UIKit                               0x00a7aa16 -[UIControl sendAction:to:forEvent:] + 69
    20  UIKit                               0x00a7ae33 -[UIControl _sendActionsForEvents:withEvent:] + 598
    21  UIKit                               0x00a7a09d -[UIControl touchesEnded:withEvent:] + 660
    22  UIKit                               0x00d7e257 _UIGestureRecognizerUpdate + 13225
    23  UIKit                               0x0099771b -[UIWindow _sendGesturesForEvent:] + 1356
    24  UIKit                               0x0099857f -[UIWindow sendEvent:] + 769
    25  UIKit                               0x0095daa9 -[UIApplication sendEvent:] + 242
    26  UIKit                               0x0096d8de _UIApplicationHandleEventFromQueueEvent + 20690
    27  UIKit                               0x00942079 _UIApplicationHandleEventQueue + 2206
    28  CoreFoundation                      0x03a6d7bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    29  CoreFoundation                      0x03a632cd __CFRunLoopDoSources0 + 253
    30  CoreFoundation                      0x03a62828 __CFRunLoopRun + 952
    31  CoreFoundation                      0x03a621ab CFRunLoopRunSpecific + 443
    32  CoreFoundation                      0x03a61fdb CFRunLoopRunInMode + 123
    33  GraphicsServices                    0x04b5a24f GSEventRunModal + 192
    34  GraphicsServices                    0x04b5a08c GSEventRun + 104
    35  UIKit                               0x00945e16 UIApplicationMain + 1526
    36  Ace Metrix MOBILE HD                0x00006cad main + 141
    37  libdyld.dylib                       0x03541ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

显示导致崩溃的代码... 有什么想法吗? 【参考方案1】:

我认为你在某处做 [label length] 而不是 [label.text length]

【讨论】:

我希望事情就这么简单。这是我搜索的第一件事。 [self.popOver setSelectionText:mdic];你可以链接 SelectorPopover 类吗?我想看看它是如何工作的。 不确定如何链接课程。复制粘贴就行了? 是的,在上面编辑您的问题并将其粘贴到那里。我真的只是想看看你的 setSelectionText 函数,但最好能看到所有这些。 因为你看不到任何明显的东西

以上是关于显示弹出框使我的 iOS 8 应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

显示弹出框时更改方向时应用程序崩溃。

iOS 5 中的弹出框变化?

输入 UISearchBar 会使我的应用程序崩溃

Swift - 呈现已呈现的弹出框时应用程序崩溃

iOS 8 弹出框宽度 = 0 在 iPad 上仅在纵向模式下

iOS:当视图位于弹出框后面时,我如何注意到?