2013-10-08 44 views
2

在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

+1

你在做什麼是正確的。並且您需要在SecondView中公開要訪問的.h文件中的內容。例如:'@property(strong,nonatomic)UISwitch * mySwitch;' – Unheilig

+0

我已經連接交換機和我的代碼使用故事板(右鍵單擊,拖動..)。但我認爲第一視圖仍然不知道第二視圖。我在viewdidload中試過這個:if(sencondview.switchON)lbl.text =「on」..它不起作用。 –

+1

如果你正在使用這一行:'SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@「secondView」];'就像你寫的,* *缺少之前「secondView = ...」 – Unheilig

回答

2

訪問它,我將發佈2班爲你在這裏:

只需複製並粘貼。

SwitchViewController.h(你的第一個視圖控制器)

@class LabelViewController; 
@interface SwitchViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch; 
- (IBAction)switch:(id)sender; 
//connect the above from storyboard. 
@property (strong, nonatomic) LabelViewController *secondView; 

@end 

SwitchViewController.m

#import "SwitchViewController.h" 
#import "LabelViewController.h" 

@interface SwitchViewController() 

@end 

@implementation SwitchViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"]; 
    self.view.backgroundColor = [UIColor lightGrayColor]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)switch:(id)sender 
{ 
    UISwitch *theSwitch = (UISwitch*) sender; 
    [self addChildViewController:self.secondView]; 
    [self.view addSubview:self.secondView.view]; 
    self.secondView.myLabel.text = (theSwitch.isOn ? @"On" : @"Off"); 
} 
@end 

LabelViewController.h(你的第二個視圖控制器)

#import <UIKit/UIKit.h> 

@interface LabelViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *myLabel; 
@property (weak, nonatomic) IBOutlet UIButton *backToFirstView; 
- (IBAction)backToFirstView:(id)sender; 
//connect the above from storyboard. 
@end 

LabelViewController.m

#import "LabelViewController.h" 

@interface LabelViewController() 

@end 

@implementation LabelViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.view.backgroundColor = [UIColor darkGrayColor]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)backToFirstView:(id)sender 
{ 
    [self removeFromParentViewController]; 
    [self.view removeFromSuperview]; 
    NSLog(@"If it works, buy me a beer."); 
} 
@end 

在故事板:創建2 UIViewControllers。 當您在那裏時,將Storyboard ID設置爲LabelViewControllersecondView

這裏是故事板的截圖:

storyboard

SUPER EDIT:

有兩類和故事板的快照。

你的firstView(具有標籤爲IBOutlet

YourFirstViewController.h

#import <UIKit/UIKit.h> 

@interface YourFirstViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *myLabel; 
@end 

YourFirstViewCntroller.m

#import "YourFirstViewController.h" 
#import "YourTableViewController.h" 

@interface YourFirstViewController() 

@property(strong, nonatomic) YourTableViewController *tableView; 

@end 

@implementation YourFirstViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    self.tableView = [self.tabBarController.viewControllers objectAtIndex:1]; 
    self.view.backgroundColor = [UIColor darkGrayColor]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"view will appear in first view"); 
    [super viewWillAppear:animated]; 
    self.myLabel.text = (self.tableView.mySwitsch.isOn ? @"ON" : @"OFF"); 
} 

YourTableViewController.h(在你有一個IBOutlet爲交換機)

#import <UIKit/UIKit.h> 

@interface YourTableViewController : UITableViewController 
@property (weak, nonatomic) IBOutlet UISwitch *mySwitsch; 
@end 

YourTableViewController。米

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"view will apear table view"); 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSLog(@"view did load in table view"); 
} 

- (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 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     [cell.contentView addSubview:self.mySwitsch]; 
    } 
// Configure the cell... 

    return cell; 
} 

storyboard snap shot

*****超級超級編輯*****

如果你想保持你的UINavigation Controller各地:

在你第一個視圖控制器.m

在'viewDidLoad'中用以下代替它:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Do any additional setup after loading the view, typically from a nib. 
    self.navController = [self.tabBarController.viewControllers objectAtIndex:1]; 
    self.tableView = (YourTableViewController*) [self.navController.viewControllers objectAtIndex:0]; 
    self.view.backgroundColor = [UIColor darkGrayColor]; 
} 

添加屬性@property(strong, nonatomic) UINavigationController *navController;

Tested. 
+0

謝謝,我會稍後再嘗試。但是當我使用靜態單元而不是普通視圖來使用UITableView時,它會有所作爲嗎? –

+0

我終於有時間嘗試你的代碼。我檢查了你的代碼,並檢查了與我的代碼有什麼不同。然後我改變了我的代碼。但它仍然無法正常工作。我使用單個視圖作爲第一個視圖,並使用UITableView作爲第二個視圖。我有一個基於標籤的應用程序。這有什麼區別嗎? OffTopic:你是德國人嗎?因爲你的名字。 –

+1

ich habe mein Beitrag editiert。 Ich habe jetzt das komplette Projekt gepostet。 Gruß –

0

首先你必須從你的UISwitch創建一個IBOutlet到你的.h文件中的settingsviewcontroller並命名爲切換器。接着使用該代碼,看看如果交換機上或:

SecondViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"]; 
BOOL onOrOff = switcher.on; 
if (onOrOff == YES) NSLog(@"Switch is on"); 
if (onOrOff == NO) NSLog(@"Switch is off"); 
0

基本上你需要做的SecondView的UISwitch變量的@property。然後通過SecondView變量