简体   繁体   English

我如何从viewdidload调用按钮的方法

[英]how i call method of button from viewdidload

I have 10 UIButton objects. 我有10个UIButton对象。 I define a single action for all of them. 我为所有这些动作定义了一个动作。 I want to animate the UIScrollView to a desired image location when I on the related button. 当我在相关按钮上时,我想使UIScrollView动画到所需的图像位置。 I have implemented this project using storyboards and I have one method for all 10 buttons. 我已经使用情节提要板实现了该项目,并且对所有10个按钮都有一种方法。 I gave a tag to each button in viewDidLoad in viewcontroller.m file like this 我像这样在viewcontroller.m文件的viewDidLoad中给每个按钮添加了标签

button1.tag = 10;
button1.tag = 11;
button1.tag = 12;
button1.tag = 13;
button1.tag = 14;
button1.tag = 15;
button1.tag = 16;
button1.tag = 17;
button1.tag = 18;
button1.tag = 19;

// viewcontroller.m file // viewcontroller.m文件

-(IBAction) Button1:(id) sender
{

 [self performSegueWithIdentifier:@"nextView" sender:sender];
}

This button method will take me to nextviewcontroller and its working fine, but in nextviewctontroller.m file i want to animate my UIScrollView to desired location. 此按钮方法将带我进入nextviewcontroller及其正常工作,但是在nextviewctontroller.m文件中,我想将UIScrollView为所需位置的动画。 I am doing it like this in nextviewcontroller.m file, but its not working. 我正在nextviewcontroller.m文件中这样做,但无法正常工作。

viewDidload
{
if ([sender tag] == 10)
{
[yourScrollView scrollRectToVisible:CGRectMake(0,0,100,100) animated:YES];
}
else if ([sender tag] == 11)
{
[yourScrollView scrollRectToVisible:CGRectMake(320,0,100,100) animated:YES];
}
/*
.
.
.
.
.
.
.
and its continue to the last tag 
*/

}

It does not animate the UIScrollView to the desired location and one thing remember here I have declared sender as type id in header file. 它不会将UIScrollView设置为动画到所需的位置,还记得我在头文件中将sender声明为type id一件事。 Kindly tell me how I can animate UIScrollView to the desired location when I click on corresponding button. 请告诉我单击相应按钮时如何将UIScrollView动画到所需位置。 I also wanted to say I did not copy and paste this code from my xcode project. 我还想说我没有从我的xcode项目中复制并粘贴此代码。 I wrote this code just give you idea what I want. 我写这段代码只是让您知道我想要什么。

At this point, you are in your second viewController and no longer have access to the tags on your buttons. 此时,您在第二个viewController中,不再可以访问按钮上的标签。 They are out of scope at that point unless you have a reference to the first view controller (which you shouldn't). 除非您引用了第一个视图控制器(否则不应该),否则它们将超出范围。

You should create a tag or offset property on your "nextViewController". 您应该在“ nextViewController”上创建标签或偏移属性。 Then, depending on how you get there from the first view controller, you need to have the first view controller set that property on the second. 然后,根据从第一个视图控制器到达那里的方式,需要让第一个视图控制器在第二个视图控制器上设置该属性。 Then you can access it in your second. 然后,您可以在第二秒钟访问它。 If you are using segue you should do this in the prepareForSegue method of the first view controller. 如果使用的是segue,则应在第一个视图控制器的prepareForSegue方法中执行此操作。

If you are pushing it on a navigation controller, you would set that property after allocating the second view controller and before pushing it onto the navigation stack. 如果要在导航控制器上推送该属性,则应在分配第二个视图控制器之后并将其推送到导航堆栈之前设置该属性。

In the header of the next view controller add this 在下一个视图控制器的标题中添加此

@property (nonatomic, strong) UIButton *buttonBeingPassedIn;

In the first View controller you want to create a UIButton globally so you can access it in any method in that file, call it something like: buttonThatWasTapped. 在第一个View控制器中,您要全局创建一个UIButton,以便可以通过该文件中的任何方法访问它,将其命名为:buttonThatWasTapped。 Then you can do something like this: 然后,您可以执行以下操作:

-(IBAction) Button1:(id) sender
{
    buttonBeingPassedIn = (UIButton *)sender;
    // Notice this has been changed to sender: self
    [self performSegueWithIdentifier:@"nextView" sender:self];
}

In the first view controller you need to add this method 在第一个视图控制器中,您需要添加此方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
      nextViewController *vc = (nextViewController *)segue.destinationViewController;
      vc.buttonBeingPassedIn = buttonThatWasTapped;
}

Then you will be able to do 那你就可以做

viewDidload
{
    if (buttonBeingPassedIn == 10)
}

Try doing this in viewWillAppear instead of viewDidLoad. 尝试在viewWillAppear而不是viewDidLoad中执行此操作。 You shouldn't put geometry-related stuff in viewDidLoad as the view's geometry (frame etc) is not set at that point. 您不应将与几何相关的内容放入viewDidLoad中,因为此时未设置视图的几何(框架等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM