简体   繁体   English

如何通过故事板将UISwitch链接到IOS7中的UITableViewController?

[英]How to link UISwitch to UITableViewController in IOS7 via storyboard?

The Setup: I have a tableviewcontroller class and I have a custom View Cell class that defines whats in it. 设置:我有一个tableviewcontroller类,并且有一个自定义的View Cell类,用于定义其中的内容。 I used storyboard to configure it and link everything. 我使用情节提要对其进行配置并链接所有内容。

I have some persistent state for the switch in my core data model and the Entity and its associated Class, lets call it "Chain". 我在核心数据模型和Entity及其关联的Class中有一些针对交换机的持久状态,我们称其为“ Chain”。 The reason I mention that is to confirm that I have persistent state for my Switch state and its built into my Core data and the data is getting grabbed just fine. 我提到它的原因是为了确认我的Switch状态具有持久状态,并且该状态已内置在我的Core数据中,并且可以很好地获取数据。

The Problem: 问题:

I have to set the state of the switch in my CoreData (specifically in my fetchedresults controller) and to save it, I need access to the particular object that i use to give TableViewCell its values and I don't know how to grab it. 我必须在CoreData中设置开关的状态(特别是在fetchedresults控制器中),然后保存它,我需要访问用于给TableViewCell提供其值的特定对象,但我不知道如何获取它。 The solutions I have seen so far seem to be linked to programmatically creating the switch within the viewcontroller class, where as I have a storyboard created switch linked to a TablViewCell thats been assigned to it. 到目前为止,我所看到的解决方案似乎与在viewcontroller类中以编程方式创建开关相关联,因为在我那里,有一个故事板创建了一个与分配给它的TablViewCell关联的开关。

I originally thought that just making it available through my TableViewCell class and referencing to it would do the trick: ie: - (IBAction)set_switchStatus:(id)sender { 我原本以为只要通过TableViewCell类使其可用并对其进行引用就可以达到目的:即:-(IBAction)set_switchStatus:(id)sender {

UITabBarController *tab = (UITabBarController *) self.window.rootViewController;
UINavigationController *nav = (UINavigationController *) [[tab viewControllers]objectAtIndex:0];
ChainMainTableView *ctvc = (ChainMainTableView *)[[nav viewControllers]objectAtIndex:0];

Chain *chain = ctvc.chain;

NSLog(@"%@", chain);



//Call my API


}

Going Over to my TableView Controller Class, my cellForRowAtIndexPath looks like this: (Note: chain is a property I added to the class of type Chain so I could refer to it easily from other classes. 转到TableView Controller类,我的cellForRowAtIndexPath看起来像这样:(注意:chain是我添加到Chain类型的类中的一个属性,因此我可以轻松地从其他类中引用它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ChainCell";
//PLACE WHERE I CHANGED UITABLEVIEWCELL
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// Save the chain for use in other classes and also Configure the cell...
self.chain = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.chainValue.text = self.chain.activity;
 cell.repsDueValue.text = [NSString stringWithFormat:@"%@ reps", self.chain.reps_due];
cell.activity_status.on = self.chain.activity_status.boolValue;


if (cell.activity_status.isOn)
{
    cell.activity_status.enabled = NO;
}
else
{
    cell.activity_status.enabled = YES;
}

return cell;

} }

So All I really need is my UISwitch to refer to the specific Object thats providing data to the cell in that row. 因此,我真正需要的只是UISwitch引用向该行中的单元格提供数据的特定对象。 Then, I need to use an identifier value in that to set my CoreData values accordingly. 然后,我需要在其中使用一个标识符值来相应地设置我的CoreData值。 I haven't got it to work yet, ANY tips would be much appreciated. 我还没有开始运作,任何提示将不胜感激。

Edit: 编辑:

So I liked the Subclassing idea, but it would have gone a bit against the spirit of the separation I was trying to get with all the classes. 因此,我喜欢Subclassing的想法,但这与我试图在所有类中实现的分离精神背道而驰。 I think I have a solution given some more reading up on stack and fin-nicking and it looks like this: 我认为我有一个解决方案,它提供了更多有关堆栈和鳍刻痕的信息,它看起来像这样:

UITabBarController *tab = (UITabBarController *) self.window.rootViewController;
UINavigationController *nav = (UINavigationController *) [[tab viewControllers]objectAtIndex:0];
ChainMainTableView *ctvc = (ChainMainTableView *)[[nav viewControllers]objectAtIndex:0];
CGPoint hitPoint = [self convertPoint:CGPointZero toView:ctvc.tableView];
NSIndexPath *hitIndex = [ctvc.tableView indexPathForRowAtPoint:hitPoint];

self.currentChain = [ctvc.fetchedResultsController objectAtIndexPath:hitIndex];

And now I have everything I need to get things to work the way i want. 现在我拥有了使事情按我想要的方式工作所需的一切。 I m not sure if its very efficient, but it gets the job done without ( im pretty sure) breaking anything. 我不确定它是否非常有效,但是可以完成工作而不会破坏任何东西。

I suggest sub-classing UITableViewCell so that it contains the UISwitch and a chain property that can be filled when the cell is created in cellForRowAtIndexPath: . 我建议对UITableViewCell进行子类化,使其包含UISwitch和一个chain属性,当在cellForRowAtIndexPath:创建单元格时可以填充该属性。 When the UISwitch's value is changed, the custom cell can fire off either a NSNotification to an interested observer, or save it to the managedContext itself, &c. 更改UISwitch的值后,自定义单元可以向感兴趣的观察者触发NSNotification,或将其保存到ManagedContext本身&c。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM