For some reason I cannot get an image album into my UIImageView. It is added in storyboard and declared:
#property (strong, nonatomic) IBOutlet UIImageView *displayAlbum;
#synthesize displayAlbum, titleLbl, artist;
In my code:
displayAlbum = [[UIImageView alloc] init];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:#"1079287588763212246" forProperty:MPMediaEntityPropertyPersistentID];
MPMediaQuery *query = [[MPMediaQuery alloc] init];
[query addFilterPredicate: predicate];
NSArray *queryResults = [query items];
for (MPMediaItem *song in queryResults) {
MPMediaItemArtwork *artwork = [song valueForProperty: MPMediaItemPropertyArtwork];
[displayAlbum setImage: [artwork imageWithSize:CGSizeMake(displayAlbum.frame.size.width, displayAlbum.frame.size.height)]];
}
I am able to get other song information so its definitely getting the right song, but the ImageView always shows up blank.
On a side note, if anybody could help me clean up the above bit of code, especially get rid of the for loop (it should always only return one result anyways) that would be great. Thanks
It's because you're creating a new image view with alloc init, instead of using the one you created in IB. You should just delete that alloc init line.
Why are you allocating displayAlbum, when it is IBOutlet? If it is already made in NIB, then by allocating new one, it should not work, as you will have completelly new UIImageView which is not part of your view controller. If it is not made in NIB, then you should add it to your view controller by adding subview of some of your views that are part of your view controller and set some frame for it.
At first, try to comment this line:
displayAlbum = [[UIImageView alloc] init];
Related
i am sending a PFImageView*(Parse.com subclass of UIImageView)* to a new View, where i am adding it as a subview, this ofc leads to the PFImageView getting detached from its previous view, which id like to avoid.
I have to call Copy, and CopyWithZone on the PFImageView, but the class does not seem to support that method, i have also tried the following:
UIImage *copiedImage = [[UIImage alloc] initWithCGImage:[[sender image] CGImage]];
PFImageView *copiedImageView = [[PFImageView alloc] initWithImage:copiedImage];
[segue.destinationViewController setFullSizeImage:copiedImageView];
But this leads to an empty PFImageView? Can i add a category to PFImageView and implement the CopyWithZone protocol ? And if so, how would i implement it?
fixed it by implementing a category with the following:
-(id)copyPFImageView
{
PFImageView *copy = [[PFImageView alloc] init];
[copy setFile:[self file]];
[copy setImage:[self image]];
return copy;
}
Remember that the data in file is quite essential
I am trying to update the score of one of the labels on my custom cell after returning from a push navigation.
In my parent UITableViewController I have the code:
In ViewDidLoad
//These are mutable strings
self.disciplineScoreString1 = [NSMutableString stringWithFormat:#""];
self.disciplineScoreString2 = [NSMutableString stringWithFormat:#""];
self.disciplineScoreString3 = [NSMutableString stringWithFormat:#""];
self.disciplineScoreArray = [[NSMutableArray alloc] init];
[self.disciplineScoreArray addObject:self.disciplineScoreString1];
[self.disciplineScoreArray addObject:self.disciplineScoreString2];
[self.disciplineScoreArray addObject:self.disciplineScoreString3];
So far so good.
In cellForRowAtIndexPath
//this is the custom subclass of UITableViewCell
DayTableViewCell *cell = (DayTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//this is the custom label
cell.disciplineScoreLabel.text = [self.disciplineScoreArray objectAtIndex:indexPath.row];
return cell;
Still so far so good. Right now the label in each of the 3 cells is blank.
I now segue to a UIViewController from the first cell and I return from the child UIViewController successfully setting the string value of self.disciplineScoreString1 to #"10"which I NSLog'ged in the parent UITableViewController.
How do I update the label for the first cell now? I have tried reload data in ViewWillAppear but its not working.
Thankyou
EDIT
This is the code in the Child ViewController
In viewWillDisappear:
[super viewWillDisappear:animated];
[self calculateDisciplineScore];
NSInteger currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
DisciplineTableViewController *parent = (DisciplineTableViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex];
parent.disciplineScoreString1 = self.disciplineScoreText;
You must be changing the string, instead of modifying it... consider this example
NSMutableArray *array = [NSMutableArray array];
NSMutableString *disciplineScoreString1 = [NSMutableString stringWithFormat:#"original string"];
[array addObject:disciplineScoreString1];
[disciplineScoreString1 appendString:#" hello"]; // OK.. you're modifying the original string
NSLog(#"%#", [array objectAtIndex:0]);
disciplineScoreString1 = [NSMutableString stringWithFormat:#"new string"]; // WRONG!!
NSLog(#"%#", [array objectAtIndex:0]);
The ouput is:
2014-02-15 06:36:46.693 Hello[19493:903] original string hello
2014-02-15 06:36:46.694 Hello[19493:903] original string hello
The second example is wrong because you're creating a new string, and the object in the array is still pointing to the old original string.
EDIT:
// try this
[parent.disciplineScoreString1 replaceCharactersInRange:NSMakeRange(0, parent.disciplineScoreString1.length) withString:self.disciplineScoreText];
// instead of
parent.disciplineScoreString1 = self.disciplineScoreText;
Your current approach is quite rigid and not very standard. You would normally set up some kind of delegate to pass data from a child to a parent. It is very rarely correct that a child should know how to reach into the parent's innards and change it's data.
I would personally start with something like this
#interface ChildViewController : UIViewController
#property (nonatomic, copy) void (^onCompletion)(ChildViewController *viewController, NSString *disciplineText);
#end
Now I'm assuming you are dismissing by just calling [self.navigationController popViewControllerAnimated:YES] from within the child? This is a little odd as it means that the childViewController can now only ever be presented in a navigationController, this makes it less reusable.
What I would do is at the point where you normally call popViewControllerAnimated: I would call the completion instead
if (self.onCompletion) {
self.onCompletion(self, self.disciplineText);
}
Now the object who provides the onCompletion can decide how this controller get's removed and it's told about the data that we finished with, which enabled the controller to do what it wants with it.
In your case the parent controller would provide the completion as it knows how the child is presented so it will know how to dismiss it. It also know that it may want to do something with the data the child finished with
// Where you present the child
childViewController.disciplineText = self.self.disciplineScoreArray[index];
__weak __typeof(self) weakSelf = self;
childViewController.onCompletion = ^(ChildViewController *viewController, NSString *disciplineText){
weakSelf.disciplineScoreArray replaceObjectAtIndex:index withObject:disciplineText];
[self.navigationController popViewControllerAnimated:YES];
[weakSelf.tableView reloadRowsAtIndexPaths:#[ [NSIndexPath indexPathForRow:index inSection:0] ]
withRowAnimation:UITableViewRowAnimationFade];
};
I can't find an obvious answer this. I'm trying to add things to an array, so I assume I need to use an NSMutableArray
I have a ViewController (CVDownload) and and a TableViewController(CVTableViewController). The NSMutableArray is declared in CVTableViewController.h
#property (strong, nonatomic) NSMutableArray *cvFiles;
I then try to add a string to it in the CVDownload.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CVTableViewController *controller = (CVTableViewController *)segue.destinationViewController;
[controller.cvFiles addObject:(#"sdsd")];
}
That doesn't work. I'm assuming because I haven't initialised the array. I've tried initialising the array in CVDownload.m
NSMutableArray *cvFiles = [NSMutableArray array];
but that doesn't work either. In debug, the array is still nil. I don't understand where I'm going wrong.
Basically, my goal is to have an array in CVTableViewController that is used to populate a Table, and I want to be able to add to the array in CVDownload. Is there something I'm missing? Does NSArray have a similar method to NSString's stringByAppendingString?
In the init method of CVTableViewController write this:
self.cvFiles = [NSMutableArray new];
CVTableViewController *controller = (CVTableViewController *)segue.destinationViewController;
if(nil == controller.cvFiles)
{
controller.cvFiles = [[NSMutableArray alloc] init];
}
[controller.cvFiles addObject:(#"sdsd")];
Where are you initializing the array? Are you sure that it is created before prepareForSegue: is called?
// Edit: I ask because, as confirmed in the comments, the array is initialized after prepareForSegue: is called. The fix, as mentioned there, is to initialize the array in -awakeFromNib instead of -viewDidLoad.
I try to store some Map Informations into a NSMutableArray but it is not working. I think there is only a minor problem that I couldn't find.
At .h File:
#property (retain, nonatomic) NSMutableArray *annotationArray;
at .m File:
MapPoint *placeObject = [[MapPoint alloc] initWithName:title subtitle:subtitle coordinate:loc];
[annotationArray addObject:placeObject];
NSLog(#" count: %lu", (unsigned long)[annotationArray count]); // always "0"
what did I wrong?
It seems that you may have forgotten to initialise the array. Try to add the following line before trying to add an object to it:
[self setAnnotationArray:[[NSMutableArray alloc] init]];
I would need to see more code, but I think it's most likely your not setting up annotationArray. Are you writing _annotationArray = [[NSMutableArray alloc] init] anywhere?
If not, the place for it is the class init method, or you could write in the function:
if (annotationArray == nil) {
_annotationArray = [[NSMutableArray alloc] init]
}
You could also use [self setAnnotationArray:[[NSMutableArray alloc] init]] instead of _annotationArray, but it depends on your synthesize statement.
I would suggest that you don't just initialize it in your function without checking if it's already been set first, because then you run the risk of overriding something else when your code gets more complex.
I think you missed this:
_annotationArray=[[NSMutableArray alloc]init];
at .m File: add after header
#synthesize annotationArray=_annotationArray;
- (void)loadView
{
self. annotationArray =[NSMutableArray array];
}
I have a UIView created in interaface builder that is a subview of a scrollview. the UIView contains a button and a label. I would like to use this view as a cookie cutter so I can generate various instances of this view that are aligned next to each other in the scrollview
I can do this programatically but that means I have to progamatically define the view size and subviews programatically, what I would prefer to do is define one instance of the view in interface builder so I can lay it out and then programatically create copies of this view. The goal is to use interfacebuilder as much as possible for defining layouts to reduce the code that I need to write.
I created a UIView category to handle this.
#interface UIView (JLTDeepClone)
- (id)deepClone;
#end
#implementation UIView (JLTDeepClone)
- (id)deepClone
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:self forKey:#"view"];
[archiver finishEncoding];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
UIView *result = [unarchiver decodeObjectForKey:#"view"];
[unarchiver finishDecoding];
return result;
}
#end
Have a look at the Entity Framework it sounds like what you are looking for and should help reduce the lines of code
http://msdn.microsoft.com/en-US/data/ef