【发布时间】:2013-10-08 14:03:10
【问题描述】:
我是 iOS 编程的新手(我知道 java),但我在使用简单的 UISwitch 时遇到了麻烦。我有一个带有两个视图的基于选项卡的应用程序。第一视图:数据(单一视图)。第二个视图:设置(TableView)。
我开始在我的第一个视图中创建一些 UILabel,并在设置视图中创建一些 TableView 单元格。现在我只希望当开关(在表格视图单元格中)打开时,第一个视图上的标签应该说“是”,否则为“否”。只是一些非常简单的事情。
我的问题是:如何在 FirstView.m 中访问我的 UISwitch?我已经导入了 SecondView.h。但是我的 FirstView 怎样才能访问 SecondView 中的所有内容呢?
我在谷歌上搜索发现:SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];
我将 SecondView 的 StoryboardID 设置为 secondView。但它不起作用。
有人可以帮帮我吗?
编辑:
这是我的代码:
FirstViewController 视图确实出现了方法:
-(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;
我通过右键将其拖到源代码中来添加它。
所以我似乎无法访问该课程
+++++++++++++++++++ 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; -
我已经使用故事板(右键单击,拖动..)将 Switch 与我的代码连接起来。但我认为第一种观点仍然不知道第二种观点。我在 viewdidload 中试过这个: if (sencondview.switchON) lbl.text="on" .. 它不起作用。
-
如果你使用这一行:
SecondViewController secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"secondView"];就像你写的那样,在 "secondView = ..." 之前缺少一个 * -
Xcode 给了我带有 * 的解决方案,但它仍然不起作用。还有什么问题?
-
@user2710855 试试我的回答,看看控制台中是否出现任何内容
标签: iphone ios objective-c uitableview