Passing A Delegate In prepareForSegue Method - core-data

I am having a problem passing an object from a TableView to a ViewController in an IOS app. I am using storyboard and have elected ARC and passing the delegate in my "prepareForSegue" method.
Here is my code in my TableView which segues via a push to another ViewController:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NextViewController *vc = (NextViewController *)[segue destinationViewController];
vc.managedObjectContext = managedObjectContext;
if ([[segue identifier] isEqualToString:#"EditCategory"])
{
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// I get a red warning which states "Cast of 'NSInteger' (aka 'int') to 'Entity' is disallowed with ARC" on this line:
[vc setEntity:(Entity *)selectedIndex];
}
}
Does anybody have any suggestions for how I can pass my object from the TableView to the ViewController? I am new to programming and have tried various expressions but nothing seems to work.

The error you're getting has to do with types.
The class NextViewController apparently has a method called -setEntity: that takes an object of type Entity *. The error is because you're trying to give -setEntity: an argument of the wrong type. You're trying to give it an NSInteger (which is a number like 0, 5, -999), but it wants an Entity.
You're on the right track for passing data from the table view to the NextViewController. You just need to do one of the following:
pass an Entity to -setEntity: (does the Entity class perhaps
have a constructor that takes an NSInteger?)
add a method to NextViewController that takes an NSInteger, and call that instead of -setEntity:

Related

Unable to instantiate NSFetchedResultController with generic type AnyObject in Swift 3

I'm experimenting with CoreData in Swift 3 and have come up against a very bizarre circular compiler error in Xcode 8 beta.
NSFetchedResultsController needs a generic type parameter and AnyObject has worked fine up until now. The compiler throws the error:
Type 'AnyObject' does not conform to protocol 'NSFetchRequestObject'
To make me extra confused, if you delete the type parameter, XCode then says:
Reference to generic type NSFetchedResultsController requires argument in `<...>`
and helpfully suggests a fix using <AnyObject>....and the cycle repeats.
This looks very much like a bug. Any ideas before I report it?
If you take a look into NSFetchedResultsController, you can clearly see that it has a parameter with name ResultType which conforms to NSFetchRequestResult. So you should pass a type which conforms to NSFetchRequestResult.
So if you take a look into NSFetchRequestResult, you can see that it conforms to NSObjectProtocol. Also NSDictionary, NSManagedObject and NSManagedObjectID conforms to NSFetchRequestResult.
public protocol NSFetchRequestResult : NSObjectProtocol {
}
extension NSDictionary : NSFetchRequestResult {
}
extension NSManagedObject : NSFetchRequestResult {
}
extension NSManagedObjectID : NSFetchRequestResult {
}
So it clear that you should pass a type from any of these three NSDictionary or NSManagedObject or NSManagedObjectID.
Create your instance of NSFetchedResultsController like this.
let resultsController : NSFetchedResultsController<NSManagedObject>!
or like this
let resultsController : NSFetchedResultsController<NSManagedObjectID>!
or like this
let resultsController : NSFetchedResultsController<NSDictionary>!
Any entity in your Core Data model maps as a subclass of NSManagedObject generated in your code so they all can be used to replace AnyObject, they all conform indirectly to NSFetchRequestResult protocol. You should see which entity/class is being fetch by your FetchRequest connected to this FetchedResultsController and that's the type you should use there.

KVO notification with Core Data in Swift

When I generate an NSManagedObject subclass with swift, the property types are all #NSManaged, meaning I can't observe them. This is a problem when using bindings in a Cocoa application because updating the property frequently requires other properties to be 'updated'.
For example, if I add this method to my NSManagedObject subclass:
dynamic var ratePerPoint: Double {
guard pointsPerMonth > 0 else { return 0 }
return monthlyRate / Double(pointsPerMonth)
}
Then it's important that whenever I update the pointsPerMonth variable, which is part of the core data object, that I send a didChangeValueForKey("ratePerPoint") message.
If I don't, then the UI bindings don't update properly.
If ratePerPoint is a calculated property you have to implement keyPathsForValuesAffectingRatePerPoint in your NSManagedObject subclass.
+ (NSSet *)keyPathsForValuesAffectingRatePerPoint {
return [NSSet setWithObjects:#"monthlyRate", #"pointsPerMonth", nil];
}
Documentation: Registering Dependent Keys

Acumatica - An object Reference is Required or the non-static field, method, or property

hi does anyone encountered this error? everytime I use PXSelect on a foreach loop in which on the other source code does but on my code does not, could anyone identify the cause? the code below is also the the original source code from Acumatica but I only changed the Datamember from PaymentCharges to OtherCharges
[PXOverride]
public void VoidCheckProc(ARPayment doc)
{
foreach (PXResult<ARPaymentChargeTran> paycharge in PXSelect<ARPaymentChargeTran, Where<ARPaymentChargeTran.docType, Equal<Required<ARPayment.docType>>, And<ARPaymentChargeTran.refNbr, Equal<Required<ARPayment.refNbr>>>>>.
Select(this, doc.DocType, doc.RefNbr))
{
ARPaymentChargeTran charge = PXCache<ARPaymentChargeTran>.CreateCopy((ARPaymentChargeTran)paycharge);
charge.DocType = Document.Current.DocType;
charge.CuryTranAmt = -1 * charge.CuryTranAmt;
charge.Released = false;
charge.CuryInfoID = Document.Current.CuryInfoID;
charge.CashTranID = null;
//PaymentCharges.Insert(charge);
OtherCharges.Insert(charge);
}
}
I believe, you are writing this method in an extension for the base BLC
So instead of using 'this', use 'this.Base'
The Select method is non-static, as the error message says, but you call it on the PXSelect<...>-type. You need to have an instance of that type.
Based on Hybridzz answer, I assume you used the wrong overload of the Select-method. Probably your arguments do not have the correct type, so the compiler selects the best fitting overload of the method. In this case, it selects the one accepting only the argument params object[] o, which is non-static. A bit misleasing design of the API you use.

monotouch DialogViewController RefreshRequested "System.ArgumentException: You should set the handler before the controller is shown"

I have some problems with getting the RefreshRequested event to work in one of my ViewControllers that implements the DialogViewController:
public CustomViewController () : base (null, true) {
RefreshRequested += delegate {
...
ReloadComplete ();
};
}
I am calling the CustomViewController from another ViewController like this:
var dvc = new CustomViewController();
this.ActivateController(dvc);
The error message I get is "Toplevel exception: System.ArgumentException: You should set the handler before the controller is shown"
Any pointers of what I am doing from here? Thanks
It looks like you do not have a RootElement specified, i.e. it's set to null by your own constructor, so you get warned that the internal state is not ready to set the event.
You should create an empty RootElement with your constructor and, later, add stuff to it (using the property). That should allow you to set the event in your own constructor. E.g.
public CustomViewController () : base (new RootElement (String.Empty), true)
Any pointers of what I am doing from here?
In doubt you can always see the entire source code MonoTouch.Dialog in it's github repository.
From my testing, the only place that you can set the event handler is in the constructor of the ViewController, as that's the only place where you can rely on the fact that the TableView property is null. I've tried the suggestion above of setting the RootElement in the constructor, but then always seem to have a TableView object before I can set the event handler. The problem with setting the event handler in the constructor though is that I don't have any way of resetting the event handler after cleaning it up.

Problem with UICustomSwitch,

I am using UICsutomSwitch for my application. When I try to create it, I am getting an exception like,
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSArray objectAtIndex:]: index 2 beyond bounds for empty array'
My code is as follows,
UICustomSwitch.h
#import <Foundation/Foundation.h>
#interface UICustomSwitch : UISwitch
{
}
-(void)setLeftLabelText:(NSString *)labelText;
-(void)setRightLabelText:(NSString *)labelText;
#end
UICustomSwich.m
#import "UICustomSwitch.h"
#implementation UICustomSwitch
-(UIView *)slider
{
return [[self subviews ] lastObject];
}
-(UIView *)textHolder
{
return [[[self slider] subviews]objectAtIndex:2];
}
-(UILabel *)leftLabel
{
return [[[self textHolder] subviews]objectAtIndex:0];
}
-(UILabel *)rightLabel
{
return [[[self textHolder] subviews]objectAtIndex:1];
}
-(void)setLeftLabelText:(NSString *)labelText;
{
[[self leftLabel] setText:labelText];
}
-(void)setRightLabelText:(NSString *)labelText
{
[[self rightLabel]setText:labelText];
}
#end
View Controller:
UICustomSwitch* switchView=[[[UICustomSwitch alloc]initWithFrame:CGRectMake(200,5,90,30)]autorelease];
[switchView setLeftLabelText:#"F"];
[switchView setRightLabelText:#"M"];
[switchView addTarget:self action:#selector(genderAction:) forControlEvents:UIControlEventValueChanged];
[elementView addSubview:switchView];
I am getting exception at " return [[[self slider] subviews]objectAtIndex:2];" call. I don't know what is the wrong, Can you guys please suggest me on this.
Thanks in advance,
Sekhar.
I ran across this issue and found the answer here:Custom UISwitch Text
Basically in iOS 4, there were UILabel's in the UISwitch's subviews that represented the "On/Off" labels. In iOS 5, there are no UILabels in the subviews array (hence the array error you're getting). The above link offers an external class you can download and customize. In my opinion, it seems like Apple is discouraging customization of UISwitch. The functionality you're after could be accomplished another way (segmented control, simulated checkbox, etc).
Also, in the given link above, the author proposes that the issue might be with not including armv6. I tried this and it does not fix the problem.
The exception indicates you are attempting to access an element of the array that is out of bounds (in a place that is larger than the actual size of the array).
You can use breakpoints and/or NSLog() calls carefully placed in your code to determine if this array ever is not-empty, and if that is so, you can continue to use these calls to find out just where the array becomes empty.
Clearly if the array is empty then the switch is setup differently than you expect it to be.

Resources