2013-04-30 82 views
0

我正在开发一个iPad应用程序。在某个阶段,我需要使用下拉式功能。所以,我使用UIPopoverView相同。从其他视图更新UIButton标题...?

当IBAction触发特定的UIButton时,我调整popoverview呈现UITableViewController。

而且一切工作正常。我需要当用户点击任何一个单元格时,相关的单元格值需要在附加的UIButton标题中设置。

enter image description here

这里,酥料饼的视图是的UITableViewController视图,它予单独创建。并在选择Outlet IBAction时调用它。

CGRect dropdownPosition = CGRectMake(self.btnOutlet.frame.origin.x, self.btnOutlet.frame.origin.y, self.btnOutlet.frame.size.width, self.btnOutlet.frame.size.height); 
[pcDropdown presentPopoverFromRect:dropdownPosition inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 

由于

+0

从表格视图更新您的按钮文本做选择方法 – 08442 2013-04-30 12:06:30

+0

感谢您的回复,是的,我想要的东西。但我不知道如何实现这一点。请帮我一些代码。 – 2013-04-30 12:08:27

+0

从表视图调用委托方法确实选择了您的主视图控制器,传递所需的信息,然后更新委托方法内的按钮。 – 2013-04-30 12:20:03

回答

4

Sangony答案几乎是正确的,但有一些细微的变化,而不是注册方法不带参数作为观察员,你应该增加它承认一个参数:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(someAction:) 
              name:@"ButtonNeedsUpdate" 
              object:nil]; 

然后,当你发布notifica重刑(在你的表的观点didSelectRow:atIndexPath :),你可以添加一个对象(NSDictionay)作为用户信息:

//... 
NSDictionary *userInfoDictionary = @{@"newText":@"some text"}; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonNeedsUpdate" 
                object:self 
                userInfo:userInfoDictionary]; 
//... 

然后在被观察​​此通知的类,可以用数据进行工作在someAction动作方法是这样的:

-(void)someAction:(NSNotification)notification{ 
    NSString *textForTheButton = [[notification userInfo]objectForKey:@"newText"]; 
    [self.myButton setTitle:textForTheButton 
        forState:UIControlStateNormal]; 
    //... 
} 

你的按钮标题现在应该是“一些文本”。

+0

优雅而简单,比设置单身。 – sangony 2013-04-30 16:14:36

+1

需要删除自己作为观察员或将崩溃。 – Mundi 2013-05-01 15:36:51

1

实现与像didSelectItemWithTitle:的方法的代表协议。使视图控制器将按钮控制在弹出窗口中的视图控制器的委托。当选中一行时,通知可以更新该按钮的委托人。

// MainController.h 
#include "PopupTableController.h" 
@interface MainController : UIViewController <PopUpListDelegate> 

// PopupTableController.h 
@protocol PopUpListDelegate; 
@interface PopupTableController : UITableViewController 
... 
@property (nonatomic, assign) id <PopUpListDelegate> delegate; 
... 
@end 

@protocol PopUpListDelegate 
-(void)didSelectItem:(NSUInteger)item; 
@end 

// PopupTableController.m 
// in didSelectRowAtIndexPath: 
if (self.delegate) { 
    [self.delegate didSelectItem:indexPath.row]; 
} 

// MainController.m 
// where you push the table view or prepareForSegue 
popupTableController.delegate = self; 

// and 
-(void)didSelectItem:(NSInteger)item { 
    // update the button based on item 
} 
+0

我尝试你的解决方案,但有一些错误** self.delegate didSelectItem:indexPath.row]; **告诉没有已知的实例方法选择器'didSelectItem'在** PopupTableController.m **以及**不完整的实现* *。 – 2013-04-30 13:00:08

+0

您必须设置委托并实施该方法。这一切都在上面指出。 – Mundi 2013-04-30 13:03:05

+0

谢谢,它有帮助,但有了这个我可以管理一个UIButton标题,实际上有多个UIButton可以调用相同的UITableViewController ...!?! – 2013-04-30 13:31:18

2

尝试使用NSNotificationCenter。在包含您的按钮将该代码放在VC:

[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(someAction) 
               name:@"ButtonNeedsUpdate" 
               object:nil]; 

-(void)someAction { 
// do stuff to your button 
} 

而且在任何其他VC会使按钮进行修改,把这个代码,以使一个通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ButtonNeedsUpdate" object:self]; 
+0

感谢您的回复,但是如何识别哪些文本必须设置,导致没有** someAction的参数** – 2013-04-30 13:40:55

+0

您可以设置一个Singleton并使用它来存储字符串的值(s)为文本标签。如果您愿意,我可以发布代码。 – sangony 2013-04-30 13:55:27