0

我不太清楚发生了什么,但是当用户选择一个按钮时,我有一个显示子视图的视图。这个子视图还有一个按钮,用户可以选择取消和删除这个子视图。当所有这些发生时,当您尝试关闭子视图时,我的应用程序会崩溃。

这是我的代码:

从选择按钮时显示子视图的主视图 -

-(IBAction)sendMessage:(id)sender{
    NSLog(@"Going to send a message....");

    CGRect frameBounds = [self.view bounds];

    float frameWidth = frameBounds.size.width;
    float frameHeight = frameBounds.size.height;
    float frameX = frameBounds.origin.x;
    //float frameY = frameBounds.size.height / 2;
    float frameY = frameBounds.size.height;

    float finalY = frameBounds.size.height / 1.75;

    MessageChooserController *chooser = [[MessageChooserController alloc] initWithNibName:@"MessageChooser" bundle:nil];

    //Set the frame off the screen at the bottom
    chooser.view.frame = CGRectMake(frameX, frameY, frameWidth, frameHeight);

    [self.view addSubview:chooser.view];

    [UIView animateWithDuration:0.5 animations:^{chooser.view.frame = CGRectMake(frameX, finalY, frameWidth, frameHeight);}];
}

如您所见,这只是显示了一个新的子视图 - MessageChooserController。现在这个子视图 MessageChooserController 有一个按钮来取消这个“动作”。

MessageChooserController 的头文件:

#import <UIKit/UIKit.h>

@interface MessageChooserController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *btn_sendEmail;

@property (weak, nonatomic) IBOutlet UIButton *btn_sendText;

@property (weak, nonatomic) IBOutlet UIButton *btn_cancel;

-(IBAction)closeChooser:(id)sender;

@end

及其实现:

#import "MessageChooserController.h"

@interface MessageChooserController ()

@end

@implementation MessageChooserController
@synthesize btn_cancel, btn_sendEmail, btn_sendText;

- (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.

    /*
    [btn_cancel setBackgroundImage:[[UIImage imageNamed:@"iphone_delete_button.png"] stretchableImageWithLeftCapWidth:8.0f topCapHeight:0.0f] forState:UIControlStateNormal];
    */
    /*
    UIImage *cancelButtonImage = [[UIImage imageNamed:@"iphone_delete_button"] resizableImageWithCapInsets:UIEdgeInsetsMake(30,0,30,0)resizingMode:UIImageResizingModeStretch];

    [btn_cancel setBackgroundImage:cancelButtonImage forState:UIControlStateNormal];
    */

}

-(IBAction)closeChooser:(id)sender{
    [self.view removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

正如你所看到的,我有一个简单的方法来关闭这个子视图,closeChooser。所有这些都可以编译,但是当我选择子视图上的取消按钮时,我的应用程序崩溃了。我在任何地方都找不到关于此的任何信息。

本质上,我希望像从联系人中选择“发送消息”时那样显示视图。

4

1 回答 1

0

这样做的正确方法是调用[self presentModalViewController:viewController]父视图控制器,然后[self dismissModalViewController]在您想要隐藏它时调用。

于 2012-10-20T14:16:49.383 回答