0

我用编程代码创建了一个UIScrollView,我的视图中有一个按钮(在 中不存在UIScrollView)。

当我单击此按钮时,转到具有模态转换的下一页。我在下一页创建取消按钮,当我单击它返回主页时(此页面有UIScrollView)。

我想在单击“取消”按钮并返回主页时调用主页中的一种方法,并在此方法中更改ContentOfSet我的 ScrollView ......但不工作!!!!

这是我的代码:

主视图.m

- (void)viewDidLoad{
    UIScrollView *scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,width, height)];
    scrollbar.directionalLockEnabled = YES;
    scrollbar.backgroundColor = [UIColor whiteColor];
    scrollbar.maximumZoomScale = 1.0;
    scrollbar.minimumZoomScale = 1.0;
    scrollbar.clipsToBounds = YES;
    scrollbar.showsHorizontalScrollIndicator = YES;
    scrollbar.pagingEnabled = YES;
    [scrollbar setContentSize:CGSizeMake(scrollbar.frame.size.width * 4,scrollbar.frame.size.height)];
    scrollbar.contentOffset = CGPointMake(0, 0);
    scrollbar.delegate = self;
    [self.view addSubview:scrollbar];

    [super viewDidLoad];
}

-(void)ChangeMainScrollContentOffset{
    scrollbar.contentOffset = CGPointMake(scrollbar.frame.size.width * (3), 0);
}

此按钮位于根视图中:

- (IBAction)AddView:(id)sender{
    AddStationController *add =  [[AddStationController alloc]initWithNibName:@"AddStationController" bundle:nil];
    [add setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self.view.window.rootViewController presentViewController:add animated:YES completion:nil];
}

这个按钮是取消按钮,用于在我的主视图中返回:

- (IBAction)BackView:(id)sender{
    mainView *main = [[mainView alloc]init];
    [main ChangeMainScrollContentOffset];

    [self dismissViewControllerAnimated:YES completion:nil];
}

请指导我!我很困惑为什么不工作 ContentOffSet !!!

4

2 回答 2

0

您的第二个 viewController 应该参考以前的视图控制器。

当你这样做时:

mainView *main = [[mainView alloc]init];
[main ChangeMainScrollContentOffset];

您创建了另一个不在屏幕上的 mainView 实例,并在另一个 scrollView 上更改contentOffset :)

您应该将属性委托添加到您的 AddStationController

添加站控制器.h

@property (nonatomic, weak) mainView* delegate;

主视图

- (IBAction)AddView:(id)sender{
    AddStationController *add =  [[AddStationController alloc]initWithNibName:@"AddStationController" bundle:nil];
    >> add.delegate = self; <<
    [add setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self.view.window.rootViewController presentViewController:add animated:YES completion:nil];
}

取消按钮动作

- (IBAction)BackView:(id)sender{

    >> [self.delegate ChangeMainScrollContentOffset]; <<
    [self dismissViewControllerAnimated:YES completion:nil];
}
于 2014-07-12T12:36:06.453 回答
0

我认为您的问题是您没有将ChangeMainScrollContentOffset 方法添加到 ViewController.h 文件中。

只需将其添加到 ViewController.h 文件中,然后调用它。

于 2014-07-12T12:24:33.473 回答