在iOS編程中我是一個新手(我知道java),並且我有一個簡單的UISwitch問題。我有一個基於標籤的應用程序和兩個視圖。第一視圖:數據(單視圖)。第二個視圖:設置(TableView)。在UITableView中使用開關
我開始在我的第一個視圖上創建一些UILabel,並在設置視圖中使用了一些TableView單元格。現在我只想當開關(在表格視圖單元格中)打開時,第一個視圖上的標籤應該說「是」,否則「否」。只是一件很簡單的事。
我的問題是:如何才能訪問我的UISwitch在FirstView.m?我已經導入了SecondView.h。但是,我的FirstView如何能夠訪問來自SecondView的所有內容?
我搜索谷歌和發現:SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
設置我SecondView到secondView的StoryboardID。但它不起作用。
有人可以幫我嗎?
編輯:
這裏是我的代碼:
FirstViewController查看確實出現方法:
-(void)viewDidAppear:(BOOL)animated
{
BOOL onOff = secondView.mySwitch.on;
if (onOff){
label.text = @"On";
}
else{
label.text = @"Off";
}
}
這裏的viewDidLoad中我的firstView
- (void)viewDidLoad
{
[super viewDidLoad];
secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
}
我
。 h文件我創建了一個屬性
@property SecondViewController *secondView;
而在.m文件中,我@合成它。
我確定我設置了Storyboard ID,因爲當我鍵入不同的東西時,程序甚至不會啓動。
在SecondViewController.h我加入了開關
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
我用右鍵點擊添加它通過將其拖到源代碼。
如此看來,我不明白的類
+++++++++++++++++ EDIT2訪問:+++++++++++ +++++++++++++
這是我的完整項目。基本上它只是標籤模板,用UITableView作爲第二個視圖。 FirstView是一個單一的視圖。因此,這裏是我的項目:
FirstViewController.h:
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *lblSwitch;
@property SecondViewController *secondView;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController()
@end
@implementation FirstViewController
@synthesize secondView, lblSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidAppear:(BOOL)animated{
if (secondView.mySwitch.isOn){
NSLog(@"First View: Switch is on!");
lblSwitch.text = @"Switch is on!";
}
else {
NSLog(@"First View: Switch is off!");
lblSwitch.text = @"Switch is off!";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
@end
SecondViewController。m
#import "SecondViewController.h"
@interface SecondViewController()
@end
@implementation SecondViewController
@synthesize mySwitch;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void) viewDidAppear:(BOOL)animated {
if (mySwitch.isOn) NSLog(@"Switch is on");
else NSLog(@"Switch is off");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Configure the cell...
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
這就是所有的代碼。這裏是main.storyboard的屏幕截圖 Here is the Photo
你在做什麼是正確的。並且您需要在SecondView中公開要訪問的.h文件中的內容。例如:'@property(strong,nonatomic)UISwitch * mySwitch;' – Unheilig
我已經連接交換機和我的代碼使用故事板(右鍵單擊,拖動..)。但我認爲第一視圖仍然不知道第二視圖。我在viewdidload中試過這個:if(sencondview.switchON)lbl.text =「on」..它不起作用。 –
如果你正在使用這一行:'SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@「secondView」];'就像你寫的,* *缺少之前「secondView = ...」 – Unheilig