English 中文(简体)
从一种观点向另一种观点发出“重载”信号的最佳做法
原标题:Best practice to send "reloading" signal to UITableView from one view to another

My goal is to notify a UITableView to refresh itself every time some configurations have changed. The problem is that the configuration view is "not" on the same view that produces the signal. (Yes, I used Tabbed Application.)

目前,我使用一种全球变量在应用方法中检测变化,并用另一种观点检查。 这是罚款,但该法典无法读用,因为它非常紧凑。 是否有这样做的有利方法? 我是否错过这一方案拟订框架中的内容?

如果是这样,我就认为,一旦通知发出,应立即恢复可上诉的意见。 在该案中,我想知道,是否有可能推迟UITableView的复读,直到出现GiidAppear。

最佳回答

I would use KVO (Key Value Observing) to keep track of when it changes:

 - (void)viewDidLoad {
      [super viewDidLoad];  

      // Note that you can use the options to get the new value passed when it
      // changes if you want to update immediately.
      [configurationObject addObserver:self forKeyPath:@"configurationItem" options:0 context:nil];
 }

 - (void)viewDidUnload {
      [super viewDidUnload];
      [configurationObject removeObserver:self forKeyPath:@"configurationItem"];
 }

 // Note that I would refresh in viewWillAppear instead of viewDidAppear
 - (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      if (self.needToRefreshData == YES) {
           [self.tableView refreshData];
      }
 }

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
      if (keyPath isEqualToString:@"configurationItem") {
           [self.needToRefreshData = YES];
      }
 }
问题回答

利用代表团设计模式将数据从一名主计长转给另一名主计长。

例如,我要说的是,塔布罗克有一辆可贵的电算器上的汽车清单,你有另一种看法,即让用户在名单上添加一辆新车。 您可以允许UITableViewController

  • Adopt AddCarViewController s protocol
  • Set itself as a Delegate for AddCarViewController s protocol
  • Implement its protocol method
  • Execute the protocol method when informed

You can then let the AddCarViewController

  • Create a Protocol
  • Declare object reference Delegate with getter and setter methods
  • Define a method under that protocol
  • Inform the Delegate when the Save action is performed

检查贵邮电号以下样本代码

@interface ViewController : UITableViewController <AddCarViewControllerDelegate>

             :
             :

// The addCar: method is invoked when the user taps the Add button created at run time.
- (void)addCar:(id)sender
{
// Perform the segue named ShowAddCar
[self performSegueWithIdentifier:@"ShowAddCar" sender:self];
}

             :
             :

// This method is called by the system whenever you invoke the method     performSegueWithIdentifier:sender:
// You never call this method. It is invoked by the system.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSString *segueIdentifier = [segue identifier];

    if ([segueIdentifier isEqualToString:@"ShowAddCar"]) {

        // Obtain the object reference of the destination view controller
        AddCarViewController *addCarViewController = [segue destinationViewController];

        // Under the Delegation Design Pattern, set the addCarViewController s delegate to be self
        addCarViewController.delegate = self;

        // Instantiate a Save button to invoke the save: method when tapped
        UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                       target:addCarViewController action:@selector(save:)];

        // Set up the Save custom button on the right of the navigation bar
        addCarViewController.navigationItem.rightBarButtonItem = saveButton;

    }

}

                 :
                 :


- (void)addCarViewController:(AddCarViewController *)controller didFinishWithSave:    (BOOL)save {
                 :
                 :
}

The Sample Code for the AddCarViewController is here

@protocol AddCarViewControllerDelegate;

@interface AddCarViewController : UIViewController

@property (nonatomic, strong) IBOutlet UITextField *carMake;
@property (nonatomic, strong) IBOutlet UITextField *CarName;
@property (nonatomic, assign) id <AddCarViewControllerDelegate> delegate;

// The keyboardDone: method is invoked when the user taps Done on the keyboard
- (IBAction)keyboardDone:(id)sender;

// The save: method is invoked when the user taps the Save button created at run time.
- (void)save:(id)sender;

@end

/*
 The Protocol must be specified after the Interface specification is ended.
 Guidelines:
 - Create a protocol name as ClassNameDelegate as we did above.
 - Create a protocol method name starting with the name of the class defining the protocol.
 - Make the first method parameter to be the object reference of the caller as we did below.
 */
@protocol AddCarViewControllerDelegate
- (void)addCarViewController:(AddCarViewController *)controller didFinishWithSave:(BOOL)save;
@end

一种做法是,在环境考虑主计长发现某种变化可以显示模型的变化时,采用某种共同的类别(或许是哪类代表)来跟踪你的模型,然后当有关观点得到考虑时,即认为DidAppear被点燃时,它可以询问模型,如果当时你知道要重新载上表观点,那么该国旗是否被打上了,否则你会这样做。

另一种方式是使用通知中心,如果您的意见被装上,可以签署示范变更通知,届时该通知就悬挂了国旗,表明它需要在下台时重载表格。

hope this helps

您可以储存核心数据,并使用NSFetResult。 持受扶养人观点的主计长担任代表。 这样一来,只要数据发生变化,你的观察控制员就会受到警示。

页: 1





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签