![](/img/trans.png)
[英]Hide view controller when button pressed / Keep custom configuration of viewcontroller - Xcode
[英]Xcode on Button pressed hide label on separate ViewController
我試圖將按下的事件按鈕鏈接到第二個ViewController,以便隱藏/刪除視圖內的標簽。 我不確定如何解釋,但請看一下。
如圖所示,當按下按鈕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;
}
有什么建議么?
您也可以使用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];
可能對您有用
您可以實現相同
在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;
}
希望這可以幫助。
做一件事使UILabel的屬性
@property (retain, nonatomic) IBOutlet UILabel *label;
然后將您的firstView控制器導入第二個視圖控制器的.h文件中
在第二個viewcontroller的viewdidload方法中初始化firstview控制器
VIewController *vc = [ViewController alloc]init];
然后將此代碼放在您的按鈕操作方法中
-(void)buttonActionMethod
{
[vc.label setHidden:TRUE];
}
查看下面的代碼可能有用
在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。希望這可能對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.