gpt4 book ai didi

iphone - 在 UITableView 中使用开关

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:26 25 4
gpt4 key购买 nike

我是 iOS 编程的新手(我懂 Java),我在使用简单的 UISwitch 时遇到了麻烦。我有一个带有两个 View 的基于选项卡的应用程序。第一个 View :数据(单一 View )。第二个 View :设置(TableView)。

我开始在我的第一个 View 中创建一些 UILabel,并在设置 View 中创建一些 TableView 单元格。现在我只希望当一个开关(位于表格 View 单元格中)打开时,第一个 View 上的标签应该说"is",否则“否”。只是一些非常简单的事情。

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

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

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

有人可以帮帮我吗?

编辑:

这是我的代码:

FirstViewController View确实出现方法:

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

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

看来我没法上课

++++++++++++++++++ 编辑2:++++++++++++++++++++++++

这是我的完整项目。基本上它只是选项卡式模板,UITableView 作为第二个 View 。 FirstView是一个singleView。所以这是我的项目:

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

最佳答案

我将在这里为您发布 2 个类(class):

只需复制并粘贴即可。

SwitchViewController.h(您的第一个 View Controller )

@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(您的第二个 View Controller )

#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

在 Storyboard 中:创建 2 个 UIViewControllers。当你在那里时,将 LabelViewController 的 Storyboard ID 设置为 secondView

这是 Storyboard的截图:

storyboard

super 编辑:

有两个类和一个 Storyboard快照。

您的第一个 View (标签为 IBOutlet 的 View )

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

***** super super 编辑 *****

如果你想保留你的UINavigation Controller:

在您的第一个 View 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.

关于iphone - 在 UITableView 中使用开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19249856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com