Ios 更改视图控制器后重命名按钮
我不擅长用大段来解释,所以希望我要说的步骤(应用程序将作为其中一个步骤)能帮助我回答这个问题 步骤:Ios 更改视图控制器后重命名按钮,ios,uibutton,Ios,Uibutton,我不擅长用大段来解释,所以希望我要说的步骤(应用程序将作为其中一个步骤)能帮助我回答这个问题 步骤: 一,。打开应用程序。视图第一视图控制器 二,。按代号为“initialButton”的按钮 三,。切换到SecondViewController 四,。按名为“选择我”的按钮文本 五,。返回FirstViewController,让初始按钮文本显示“选择我” FirstViewController.h @interface FirstViewController : UIViewControll
一,。打开应用程序。视图第一视图控制器
二,。按代号为“initialButton”的按钮
三,。切换到SecondViewController
四,。按名为“选择我”的按钮文本
五,。返回FirstViewController,让初始按钮文本显示“选择我” FirstViewController.h
@interface FirstViewController : UIViewController
@property IBOutlet UIButton *initalButton;
@end
FirstViewController.m
- (void)viewWillAppear:(BOOL)animated {
self.initalButton.titleLabel.text = name;
}
selectMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[selectMe setFrame:CGRectMake(100, 100, 100, 100)];
[selectMe setTitle:@"Select Me" forState:UIControlStateNormal];
[selectMe addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
- (void) someMethod: (id)sender {
if (sender == selectMe) {
self.selectMe.titleLabel.text = name;
[self.navigationController popViewControllerAnimated:YES];
}
}
SecondViewController.m
- (void)viewWillAppear:(BOOL)animated {
self.initalButton.titleLabel.text = name;
}
selectMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[selectMe setFrame:CGRectMake(100, 100, 100, 100)];
[selectMe setTitle:@"Select Me" forState:UIControlStateNormal];
[selectMe addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
- (void) someMethod: (id)sender {
if (sender == selectMe) {
self.selectMe.titleLabel.text = name;
[self.navigationController popViewControllerAnimated:YES];
}
}
(NSObject)Background.h
NSString *name;
我的意图是,当按下selectMe按钮时,应用程序返回一个ViewController,并且initalButton的文本显示SecondViewController上按下的任何按钮。除了在SecondViewController上显示按下按钮文本的initialButton(上面的步骤5)之外,应用程序中的所有内容都正常工作您应该在两个控制器之间设置一个委托关系,这样当点击第二个控制器中的按钮时,它会在弹出前调用第一个控制器的选择信息。然后,第一个控制器可以在重新显示之前更新其状态。您要做的是设置委托关系。这允许您访问以前活动的ViewController中的属性 我假设您将在本例中使用故事板,但基本流程如下。在执行segue之前,
prepareforsgue:sender:
被调用,我们首先检查segue是否在SecondViewController上工作。如果是,那么我们就可以进入并访问它的属性。在这种情况下,我们需要的是委托。通过将self
分配给委托,这允许我们在仍然处于SecondViewController中时在FirstViewController中执行我们想要的任何操作,因为我们现在有了对父视图控制器的引用。酷吧?您可以更改属性、运行方法调用等。这是非常有用的东西,您可能会经常使用它
顺便说一下,您可能希望使用UIButton的setTitle:forState:
方法来更改标题名称
我不知道这段代码是否会编译,因为它依赖于您所做的我看不到的事情,但您至少可以了解它的工作原理。祝你好运
// FirstViewController.h
@interface FirstViewController : UIViewController
@property IBOutlet UIButton *initialButton;
@end
// FirstViewController.m
#import "SecondViewController.h"
- (void)viewWillAppear:(BOOL)animated
{
[self.initialButton setTitle:name forState:UIControlStateNormal];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *viewController = segue.destinationViewController;
if ([viewController isKindOfClass:[SecondViewController class]) {
SecondViewController *controller = (SecondViewController *)viewController;
controller.delegate = self;
}
}
// SecondViewController.h
@class FirstViewController;
@interface SecondViewController : UIViewController
@property (nonatomic, strong) FirstViewController *delegate;
@end
// SecondViewController.m
- (void)viewDidLoad
{
selectMe = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[selectMe setFrame:CGRectMake(100, 100, 100, 100)];
[selectMe setTitle:@"Select Me" forState:UIControlStateNormal];
[selectMe addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) someMethod: (id)sender
{
if (sender == selectMe) {
[self.delegate.initialButton setTitle:selectMe.titleLabel.text forState:UIControlStateNormal];
[self.navigationController popViewControllerAnimated:YES];
}
}
这帮了大忙。我最终改变了我的应用程序,将文本显示在它附近的标签上,因为应用程序从1->2->1开始,在1上查看时按钮是空白的,但我觉得在将来,如果应用程序从1->2开始,需要这样做,这应该会起作用。非常感谢。