UICollectionView with pictures - swift

I am trying to appear pictures into the cells that related to UICollectionViewCell. but for some reasons the pictures do not appear on the cells and also when I run the app is shows some errors
here's the error first.
libc++abi.dylib: terminating with uncaught exception of type NSException
and this is my codes
enter image description here
thanks

The most obvious issue would be that you have no view with tag 2 set. Since you are forcing the value, instead of checking if you actually get a value there, you might get an exception at that point if the call to viewWithTag returns nil.
Try changing this line:
var imageView = cell.viewWithTag(2) as! UIImageView
To something like this:
if let imageView = cell.viewWithTag(2) as? UIImageView {
// Set the image here
}
If you don't get the crash after that, then the issue is in that bit of code. If you still get a crash, you might want to try uploading your project somewhere and then posting a link here so that anybody interested can take a look to see what is going on.

Related

Mac OS - Change Status Item Button Image

I want to update the image when the user clicks something in the application but I can't get it to work.
The status item with the menu is defined in the AppDelegate. I am trying to update the image in the ViewController with this piece of code which I think should work:
AppDelegate().statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))
No errors are showing up, but turns out it still doesn't work, so is it possible to change the image or am I doing something wrong?
AppDelegate() creates a brand new instance which is not the delegate class in Interface Builder.
You need the real reference:
(NSApp.delegate as! AppDelegate).statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))

Unexpectedly found nil when opening an UIImage

I am developing a Walkthrough screen where I have added 3 instruction images which help the users to work with the application. The problem occurs, when I first launch the app I see only the labels with the "instruction tekst" , but below this label the user should also see the images. Maybe I am doing something wrong, attached I uploaded a picture of the error:
And here I upload the picture of the code I have used to transfer these picture to the TutorialPageContentHolderViewController:
I would be very thankful if you see my mistake, because I have been struggling with that the whole day.
I guess the imageFileName variable in your first image is nil when you use it in
myImageView.image = UIImage(named: imageFileName)
When you assign it a value in the second screenshot it could be too late, as viewdidLoad is loaded before that. You could check if this is true by manually assigning a valid value to imageFileName before using it and see if it works.
One simple way to fix it would be to add an initialiser to the view controller class, pass the filename as a parameter and store the filename in a local variable until you use it in viewDidLoad.
A second way would be to add a method to the page view controller as below (and remove the corresponding line from viewDidLoad):
func prepare(with filename:String) {
myImageView.image = UIImage(named: filename)
}
and call it from the pageTutorialAtIndex method in your main view controller.

Updating gps locations in a uitable view in the cell.detailTextLabel.text problem

I am working on a Core Location based application which displays the name of the place you are heading towards in the UITableView text and display how far away from the location you are from in the cell.detailTextLabel.text.
I have GPS delegate that updates the location and I set a Core location getter and setter and try to use that location to set the cell.detailTextLabel.text each time I get an update.
But my problem lies in scrolling the UITableView. When i click on a cell is goes to where I want it to but if I try to scroll the view to see all the cells the app crashes and issues a bad access error. I don't know where there is a bad access in the location update or if there is a better way to get the gps locations into the cell.detailTextLabel.text and update it better than get the new location and reloading the data.
any help would be greatly appreciated.
thanks
Almost certainly you have an error in your UITableViewDataSource or UITableViewDelegate methods. If you don't get the count of sections and cells right or if you don't return a valid cell as required you will always crash. Some NSLogs in these functions might help you to spot any problems as they occur.
Next make sure to set the environment variable (doubleclick the app executable, select arguments) and add "NSZombieEnabled" with value YES. If you are releasing something that your table later uses, EXC_BAD_ACCESS is the result and this will cause a debugger breakpoint rather than just a mysterious crash - with a stack trace you should be able to easily find the problem.
You're using reloadData to refresh the table view cell?

uiview hidden problems in iphone sdk

if i was hidden the image view.. run time error was occured or
imgView.hidden=YES;
imgView.hidden=NO;
this two lines are got a runtime error.. how to handle this..... or
imgView.image= any one image
imgView.image=nil
this two lines also got a run time error is "exec-bad access" or "debugging is termnated"
how to handle this, please help me
Have you create outlet for imageview or not?
If it's not outlet then you have to initialize the imageview

How do I debug an 'unrecognized selector sent to instance' problem?

I have the following code in a view controller that (in all other respects) seems to be working fine:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ProblemViewController *problemViewController = [[ProblemViewController alloc] initWithNibName:#"ProblemViewController" bundle:nil];
problemViewController.problem = (Problem*)[self.problems objectAtIndex:indexPath.row];
[self.navigationController pushViewController:problemViewController];
[problemViewController release];
}
When I run through this function, however, I am getting the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[ProblemViewController initWithNibName:bundle:]: unrecognized selector sent to instance 0x57ca80'
I'm at a loss as to what could be causing it, so my question is: How should I best go about debugging this problem? Is there anything obvious I should be checking?
Check the base class for ProblemViewController. I'm betting you're not inheriting from UIViewController, which includes the initWithNibName:bundle: method.
There are a bunch of answers to this question that all basically say the same, correct, thing: Somehow, the method you're trying to call doesn't actually exist.
BUT, if you're beating your head against the wall (like I was for an hour today), make sure you try cleaning your project in xcode first. Sometimes, I don't know why, xcode can get into a bad state and will not properly compile your project into the simulator. It will tell you the build is successful, but when deployed to the simulator, you start seeing runtime errors as though only half your changes were picked up. So yeah, that happens.
You likely don't have that method implemented in your ProblemViewController. Unrecognized selector is, as far as I know, just that there's no method defined in this class's interface that has that signature.
Try declaring it in your interface like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
I thought I better post something on this issue. I fought it for a couple of days. There are a lot of blogs on this, but none of them solved the problem. Some suggested that it had to do with pushViewController needing to specify animated:YES. It worked for some, but that didn't work for the issue I was having. I was using a button to access another view rather than selecting a row from a table view, but was getting the same unrecognized selector error. I was also using a navigation controller so I thought it had something to do with that, but it didn't. Finally, I decided to comment out lines of code till the message went away. After commenting out the method that was causing the problem, the error message still came up in the console. That is when I realized the problem was not with my source code. One site recommended performing a clean and rebuild. I tried all that and still the problem persisted. I then looked at my XIB file using Interface Builder to see what methods(Received Actions) displayed on the File Owner. There was the problem. Not only did the offending method show up once on the File Owner, but it displayed twice. Don't know how to display the image of this here. Anyway, I deleted the methods (Received Actions) with the same name that appeared on the File Owner. I performed a clean and rebuild to see if the error went away and it did. I then uncommented the source I thought was bad and built the project again and the call to the new view was successful.
I will share my experience with the same error code. It is possible to make a mistake my assigning a the object to the wrong target. For example, if you have some UILabel property and you have accidentally assigned the string constant directly to self.myUILabelProperty = #"ups" then you property will become object of type NSString instead of being UILabel, so you loose all the UILabel methods. After that mistake, if you try to use UIlabel methods on the property in code you will get this error message.