开发者

problem setting int value, unrecognized selector sent to instance.... iphone

in my application i'm trying to send an int value from one view to another view. To do this i'm setting up a navbar button programmatically and passing it an action from the same class that's creating the button. In the next view that will load i have a property that i'll set when the user clicks the navbar button. The problem is that the application crashes when i set this value. I have the int value in a @property in the view's h class and then i have the @synthesize below the @implementation line.

Here is the code for the navbar button, this is done in the initWithNibName method:

UIBarButtonItem *contentsButton = [[UIBarButtonItem alloc] initWithTitle:@"Contents"                                            style:UIBarButtonItemStyleBordered target:self action:@selector(showBookContents)];


        [[self navigationItem] setRightBarButtonItem:contentsButton];
        [contentsButton release];

Here is the method that i pass to the button when it's created:

-(IBAction)showBookContents{
    BookContentsViewController *contentsViewController = [[BookContentsViewController alloc] initWithNibName:@"BookContentsViewController" bundle:nil];

    contentsViewController.selectedBookId = self.selectedBook._id;

    [[self navigationController] pushViewController:contentsViewController animated:YES];

    [contentsViewCont开发者_Python百科roller release];

}

Here is the class that will be loaded when the use clicks the navbar button:

@interface BookContentsViewController : UIViewController {
    int selectedBookId;
}

@property int selectedBookId;

@end

Here is the implementation for the above class:

@implementation BookContentsViewController

@synthesize selectedBookId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

As you can see the @property and @synthesize looks fine. Here is the error i get:

2011-06-13 11:48:42.321 BookApp[22762:207] -[BookDescriptionViewController setSelectedBookId:]: unrecognized selector sent to instance 0x4d9f070 2011-06-13 11:48:42.323 BookApp[22762:207] ** ** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BookDescriptionViewController setSelectedBookId:]: unrecognized selector sent to instance 0x4d9f070'

What can the problem be guys?

Thanks in advance for your patience.


try this

make this selectedBookId NSString and then try.

NSString *selectedBookId;
int selectedBookIdIntegerValue;

@property(nonatomic,retain)NSString selectedBookId; 

Change in this method

-(IBAction)showBookContents{
    BookContentsViewController *contentsViewController = [[BookContentsViewController alloc] initWithNibName:@"BookContentsViewController" bundle:nil];

    contentsViewController.selectedBookId = [NSString stringWithFormat:@"%d",self.selectedBook._id];

    [[self navigationController] pushViewController:contentsViewController animated:YES];

    [contentsViewController release];

}

dont forget to release selectedBookId in dealloc of BookContentsViewController.

since selectedBookId is now NSString You might need integer value so what you do is selectedBookIdIntegerValue=[self.selectedBookId intValue];


You are calling the method on an instance of BookDescriptionViewController however it is defined in BookContentsViewController


You can pass the value by using NSInteger as:

@interface BookContentsViewController : UIViewController {
    NSInteger CurrentBookId;
}
@property(assign) NSInteger CurrentBookId;
@end


@implementation BookContentsViewController
@synthesize CurrentBookId;

Here is you bar button Action:

-(IBAction)showBookContents{
BookContentsViewController *contentsViewController = [[BookContentsViewController alloc] initWithNibName:@"BookContentsViewController" bundle:nil];

contentsViewController.CurrentBookId = self.bookId;

[[self navigationController] pushViewController:contentsViewController animated:YES];

[contentsViewController release];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜