按钮上的Xcode按下单独的ViewController上的隐藏标签

时间:2014-05-12 12:02:29

标签: ios xcode uibutton hide viewcontroller

我正在尝试将按下的事件按钮链接到第二个视图控制器,以隐藏/删除视图中的标签。我不知道如何解释,但看看。

enter image description here

如图所示,按下按钮2时,第一个视图中的主标签将被删除或隐藏。

我尝试过使用:

file.h

@interface ViewController : UIViewController {


    IBOutlet UILabel *LabelToHide;

    IBOutlet UIButton *button2;



}

-(IBAction)unhide:(id)sender;

@end

file.m

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)unhide:(id)sender{



}

- (IBAction)hide:(id)sender{

    LabelToHide.hidden = YES;
    button2.hidden = YES;

}

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

您也可以使用NSNotification

来完成此任务

ViewController课程中写入viewDidLoad

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


- (void) testMethod:(NSNotification *) notification
{
    // call your hide/unhide method
}

点击按钮

中的viewController``Pressed课程

发布通知

[[NSNotificationCenter defaultCenter] 
        postNotificationName:@"Notification_name" 
        object:self];

可能会对你有用

答案 1 :(得分:0)

你可以实现相同的

在FirstViewController.m文件中

[self performSegueWithIdentifier:@"FirstView" sender:self];

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"FirstView"]) {
        SecondView *view2 = (SecondView *)segue.destinationViewController;
        view2.labletohide = YES;
    }
}

在SecondViewController.h文件中

@property (nonatomic, assign) BOOL labletohide;
@property (nonatomic, weak) UILabel label;

在SecondViewController.m文件中

- (void)viewDidLoad
{
    if (labletohide)
        self.label.hidden = YES;
}

希望这有帮助。

答案 2 :(得分:0)

做一件事 制作UILabel的财产

@property (retain, nonatomic) IBOutlet UILabel *label;

然后在第二个视图控制器的.h文件中导入firstView控制器

在第二个viewcontroller的viewdidload方法中使用第一个视图控制器

VIewController *vc = [ViewController alloc]init];

然后将此代码放入按钮操作方法

-(void)buttonActionMethod

{
[vc.label setHidden:TRUE];
}

答案 3 :(得分:0)

查看以下可能有用的代码

ViewController.h文件中的

声明为

   #import <UIKit/UIKit.h>
   #import "DetailsViewController.h"
   @interface ViewController :<UIViewController>
   DetailsViewController *detailsViewController;
   @property(nonatomic,retain) IBOutlet UIButton *next;
   @property(nonatomic,retain) DetailsViewController *detailsViewController;
   -(void)nextbuttonClicked;
ViewController.m中的

在按钮方法

中提示以下代码
    -(void)nextbuttonClicked{

    if (self.detailsViewController == nil)
    {
    self.detailsViewController = [[DetailsViewController alloc]
                                  initWithNibName:@"DetailsViewController"
                                  bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:
    self.detailsViewController animated:YES];

     self.detailsViewController.label.hidden=YES;
    }
    }

现在将Interface Builder上的 next 按钮连接到 nextbuttonClicked 创建一个名为DetailsViewController的Xib文件,并将标签添加到IBOutlet.Hope,这可能对您有所帮助。