在UITableView中使用开关

时间:2013-10-08 14:03:11

标签: iphone ios objective-c uitableview

我是iOS编程的新手(我知道java),我在使用简单的UISwitch时遇到了麻烦。我有一个基于Tab的应用程序有两个视图。第一视图:数据(单视图)。第二个视图:设置(TableView)。

我开始在第一个视图上创建一些UILabel,在Settings-view中创建一些TableView Cells。现在我只想在Switch(在表视图单元格中)打开时,第一个View上的标签应该显示“YES”,否则为“no”。只是非常简单。

我的问题是:如何在FirstView.m中访问我的UISwitch?我已经导入了SecondView.h。但是,我的FirstView如何才能访问SecondView中的所有内容?

我在谷歌搜索并找到:SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];

我将SecondView的StoryboardID设置为secondView。但它不起作用。

有人能帮助我吗?

编辑:

这是我的代码:

FirstViewController View确实出现了Method:

    -(void)viewDidAppear:(BOOL)animated
{
    BOOL onOff = secondView.mySwitch.on;
    if (onOff){
        label.text = @"On";
    }
    else{
        label.text = @"Off";
    }
}

这是我的FirstVIew的viewDidLoad

    - (void)viewDidLoad
{
    [super viewDidLoad];

    secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
}

在我的.h文件中我创建了一个属性

@property SecondViewController *secondView;

在.m文件中我@synthesize它。

我确定我设置了Storyboard ID,因为当我输入不同的内容时,程序甚至都没有启动。

在SecondViewController.h中添加了Switch

@property (weak, nonatomic) IBOutlet UISwitch *mySwitch;

我通过右键单击将其添加到源代码中来添加它。

所以我似乎无法访问该课程

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /强>

这是我完整的项目。基本上它只是选项卡模板,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

3 个答案:

答案 0 :(得分: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。 当您在那里时,将LabelViewController的故事板ID设置为 secondView

以下是故事板的截图:

storyboard

SUPER EDIT:

有两个类和故事板的快照。

您的第一个视图(标签为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.m

- (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

第一个视图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.

答案 1 :(得分: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");

答案 2 :(得分:0)

基本上你需要在SecondView中创建一个UISwitch变量的@property。然后通过SecondView变量

访问它