使UIScrollView滚动按钮从另一个ViewController单击按钮

时间:2014-07-03 16:21:17

标签: ios xcode uiscrollview

我有一个带有3个childViewControllers的UIScrollview。 有没有办法从其中一个childViewControllers上的按钮滚动UIScrollView? 我不确定如何从孩子那里访问UIScrollView,否则我可以使用setContentOffset。

编辑:

试图实施张的答案。

在ChildViewController中

protocol ChildVCDelegate {
    func childVC(childVC: MainViewController, scrollButton:UIButton)
}

class MainViewController: UIViewController {

    var delegate: ChildVCDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.profileButton.addTarget(self, action: Selector("informDelegateToScrollMethod"), forControlEvents: UIControlEvents.TouchUpInside)
    }

    func informDelegateToScrollMethod(sender: AnyObject) {
        self.delegate?.childVC(self, scrollButton:sender as UIButton)
    }

}

然后当我尝试使用Scroll View将协议添加到Controller:

class CustomPagerViewController: PagerViewController, ChildVCDelegate {
    ...
}

我得到一个"类型CustomPagerViewController不符合协议ChildVCDelegate"

任何想法?

更新:

我能够让View控制器符合,但现在我得到了一个 行上的EXC_BAD_ACCESS(代码= 2)错误:

self.delegate?.childVC(self, scrollButton:sender as UIButton)

我有什么遗失的吗?

最终更新:

我能够通过将@objc添加到协议来实现它:

@objc protocol ChildVCDelegate {
    ...
}

1 个答案:

答案 0 :(得分:0)

也许协议可以提供帮助。

首先在子视图控制器类(ChildVC)中定义一个协议,如下所示:

@class ChildVC;

@protocol ChildVCDelegate <NSObject>

// methods your receiver delegate need to implement
-(void)childVC:(ChildVC *)childVC didPressButton:(UIButton *)scrollButton

@end

@interface ChildVC: UIViewController
{

}

@property <nonatomic, weak> id<ChildVCDelegate> delegate;

@end

然后在您的ChildVC实现文件中:

@implementation ChildVC

-(void)viewDidLoad
{
    ....
    UIButton *myButton = [[UIButton alloc] initWithFrame....];
    ...
    [myButton addTarget:self action:@selector(informDelegateToScrollMethod:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)informDelegateToScrollMethod:(id)sender
{
    // tell your delegate to execute the delegate method defined in .h file of your ChildVC
    [self.delegate childVC:self didPressButton:sender];
}

在定义了UIScrollView的主视图控制器中,如果您还没有,请转到.h文件和#import "ChildVC.h"并使主视图控制器符合ChildVCDelegate

#import "ChildVC.h"

@interface MainViewController: UIViewController <ChildVCDelegate>

最后,在您定义UIScrollView的视图控制器中,您可以使用委托方法实现执行任何操作:

@implementation MainViewController

-(void)viewDidLoad
{
    ...

    // ---------------------------------------------------------------------------
    // tell the childViewController, the mainViewController is the delegate
    // when that button is pressed inside child view controller
    // ---------------------------------------------------------------------------
    self.childViewController.delegate = self;
}

...

// -----------------------------------------------------------
// This is the delegate method that must be implemented
// -----------------------------------------------------------
-(void)childVC:(ChildVC *)childVC didPressButton:(UIButton *)scrollButton
{
    // Put logic to scroll UIScrollView here
    [self.myScrollView setContentOffset:CGSizeMake(0, 100) animated:YES];
}

希望这有效:D