在NavigationController中传回int

时间:2014-06-23 09:01:12

标签: iphone objective-c uinavigationcontroller nsinteger

我坐在一个我暂时无法解决的问题上。我一定是做错了。 我有2个观看次数,UIViewUITableView。它们嵌入了NavigationController

在第二个视图中; TableView,我想将NSInteger传递回UIView,第一个视图。

我使用了https://stackoverflow.com/a/19343715/3355194的解决方案,但对我没有用。它在UIView中给了我一个0值。在TableView中,它确实给了我numberOnSectionInTableView的正确值。 还尝试了一些其他解决方案,但结果相同。

到目前为止我做了什么。

TableView.h

@protocol senddataProtocol <NSObject>

-(void)sendBadgeIntBack:(int*)section;

@property (nonatomic, assign) id delegate;

TableView.m

@synthesize delegate;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    self.section = [self.tableView numberOfSections];
    section = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];

    NSLog(@"Number of sections: %i", section-1);

    return (int)[[[UIApplication sharedApplication] scheduledLocalNotifications] count]/2;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [delegate sendBadgeIntBack:&(section)];
}

UIView.h

 @property (nonatomic) int section;

UIView.m: 它在这里给我一个0值。

-(void)sendBadgeIntBack:(int*)section
{
    NSLog(@"The numbers of sections in tableView are: %i", self.section);
}

希望你能帮我找到解决方案。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

在TableView.h中:

@protocol senddataProtocol <NSObject>

-(void)sendBadgeIntBack:(int)section_selected;

@property (nonatomic, assign) id delegate;
 @property (nonatomic) int section;

TableView.m:

@synthesize delegate;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    self.section = [self.tableView numberOfSections];
    section = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];

    NSLog(@"Number of sections: %i", section-1);

    return (int)[[[UIApplication sharedApplication] scheduledLocalNotifications] count]/2;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [delegate sendBadgeIntBack:section];
}

UIView.h:

 @property (nonatomic) int section;

UIView.m:这里给我一个0值。

-(void)sendBadgeIntBack:(int*)section_selected;
{
self.section = section_selected;
    NSLog(@"The numbers of sections in tableView are: %i", self.section);
}

答案 1 :(得分:1)

您使用传递引用(int *)而不是传递值(int),您应该将其用于原始值。为了简化,对象得到一个星形,整数,浮点数,结构等。不要。

此外,如果问题仍然存在,您可能没有正确设置代理,在方法中放置一个断点并检查它是否被正确调用。

相关问题