0

So let's say I have a viewController view and a popup UITableView table.

I want to be able to change the background view of the viewController that calls the pop-up table by selecting an option from the table.

So what I want to do is the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    int selectedEntry = indexPath.row;
    switch(selectedEntry){
        case 1:
          //CODE TO CHANGE ORIGINAL VIEW TO IMAGE 1
        case 2:
          //CODE TO CHANGE ORIGINAL VIEW TO IMAGE 2
        //etc
   }
}

I already know that if I was writing the method directly in the base view class I could just write

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];

but since it's being called from the popup menu, I don't know how to access it.

4

4 回答 4

0

you can use NSNotificationCenter.

Add this lines.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int selectedEntry = indexPath.row;
    switch(selectedEntry){
        case 1:
          [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:self userInfo:yourData1];
        case 2:
          [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:self userInfo:yourData2];
        //etc
   }
}

Add this to BaseViewController where you want to change background

- (void)viewDidLoad {

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

}

- (void)changeBackgroundView:(NSNotification *)yourData {

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];

}
于 2013-05-30T08:31:16.277 回答
0

by using protocol you can do this like like AVC(aViewController) show BVC(bviewcontroller) as popover then you need to do is as follows in BVC.h

@protocol TablePopoverDelegate <NSObject>
-(void)doSomeWork;
@end
@property (nonatomic, weak) id<TablePopoverDelegate> delegate;

and in BVC.m you need to do is

- (IBAction)buttoonpressd:(id)sender {
    [self.delegate doSomeWork];
}

and in AVC.h you need to add this protocol :"TablePopoverDelegate"

and then in AVC.m you need to implement desired function and set delegate like as follows

do this stuff from where you present popover

here "button" is a sender by pressing it you are going to show popover

__weak UIViewController *selfView = self;

UIViewController *nextViewController;
UIViewController *infoViewController = [[UIViewController alloc] init];
            infoViewController.delegate = self;
            nextViewController = infoViewController;


_popOverController = [[UIPopoverController alloc] initWithContentViewController:nextViewController];
        [self.popOverController setDelegate:self];
        __weak UIPopoverController *blockPopoverController = self.popOverController;

        self.popoverControllerPresentationBlock = ^(void){
            [blockPopoverController presentPopoverFromRect:[button convertRect:button.bounds toView:selfView.view]
                                                    inView:selfView.view
                                  permittedArrowDirections:UIPopoverArrowDirectionUp
                                                  animated:YES];
        };
        // invoke the presentation block to show the popover now
        self.popoverControllerPresentationBlock();

and here is the function you are calling by delegate

-(void)doSomeWork
{
    //do your stuff here
}
于 2013-05-30T08:31:46.393 回答
0

当您单击弹出控制器中的按钮时,您可以使用委托来监听按钮操作并在视图控制器中执行必要的操作。或者您可以使用 NSNotification 在按钮单击期间发布通知并在视图控制器中收听通知。

在按钮动作中写下这个

[[NSNotificationCenter defaultCenter] postNotificationName:UIButtonOneClickedNotification object:nil];

在视图控制器的 didLoad 方法中写这个

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonOneClicked:) name:UIButtonOneClickedNotification object:nil];

并在方法buttonOneClicked中做你需要做的事情:

于 2013-05-30T08:21:30.340 回答
0

一个解决方案可能是UIViewController在您的UITableView子类中添加一个属性,这样您就可以保留对它的引用,然后再访问它:

@property (nonatomic, weak) MyViewController *parentVC;

MyViewController作为你的UIViewController子类)

当您UITableView从您的初始化您的时UIViewController,将其parentVC属性设置为self. 然后在 中didSelectRowAtIndexPath:,使用:

parentVC.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];
于 2013-05-30T08:18:20.937 回答