simple UIButton always crashes my app on touch - iphone

i have a strange problem.
On my view i have two segmented controls and one button.
The segmented controls work perfectly but my button crashes the app when i touch it.
NSLog won't be called anymore.
PageViewController.h
paintingOptions = [[UIButton alloc] initWithFrame:CGRectMake(200,30,100,100)];
[paintingOptions setTitle:#"Ok" forState:UIControlStateNormal];
[paintingOptions setBackgroundColor:[UIColor blackColor]];
[paintingOptions addTarget:self action:#selector(showPaintingOptions:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:paintingOptions];
-(void) showPaintingOptions: (id) sender {
NSLog(#"Button pressed");
}
Any hints on how to solve that ?
Debug:
The debug output says nothing but (gdb).
Xcode shows me this line:
PDFViewController.h
- (UIView *)view {
return self.scrollView; }
0x32c37102 <+0014> ldr r1, [pc, #16] (0x32c37114 <-[UIViewController nextResponder]+32>)
But this function is in another view controller.
So the view where i have the button in is a subview of this scrollView.
Adding PageViewController to PDFViewController:
- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
PageViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
//page+1 cause CGPDF indexing starts with 1
controller = [[PageViewController alloc] initWithPageNumberAndUrl:page+1: [chapter urlOnFilesystem]];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}

From the crash log it looks like -(void) showPaintingOptions: (id) sender is not in your target self. Please make sure that.

Related

Uipagecontrol customization in iOS

I am trying to add different views on the pages of a uipagecontroller.Have total of three pages.That is in the first page i have to implement a map,in the second view,a simple view with some labels and the in the third view,a table.I followed the tutorial available here : http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html for implementing the page controller.How can i set the views of this controller based on the selected page.?Can anyone give some idea?I tried with this method.But it didn't give any way
- (void)loadScrollViewWithPage:(int)page {
NSLog(#"%d",page);
if (page < 0)
return;
if (page >= kNumberOfPages)
return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (controller.view.superview == nil)
{
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scroll addSubview:controller.view];
}
}
Add your required views to scrollView Object, and then you can manage all views by using this code:
https://github.com/SIMHAM/DDPageControl

PageController and TableView

I have a page controller and that method pageAction defined like this :
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
[self getVehicules];
[self.tableViewVehiculesPossedes release];
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
[self loadScrollViewWithPage:page];
CGRect frame = pageControlVehiculePossedee.frame;
frame.origin.x = (vosvehiculeScrollView.frame.size.width * page);
[vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Every time I change the page I need to display a tableview with some labels. Here is the code :
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[self loadScrollViewWithPage:0];
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
votreVehiculeLabel.text=#"Votre véhicule";
vehiculesPossedesArray = [[NSMutableArray alloc] initWithObjects:#"Annee modele", #"Transmission",#"Carburant", nil];
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
tableViewVehiculesPossedes=[[UITableView alloc] initWithFrame:CGRectMake(3, 80, 315, 171) style:UITableViewStyleGrouped];
tableViewVehiculesPossedes.tag=page;
tableViewVehiculesPossedes.bounces=NO;
tableViewVehiculesPossedes.backgroundColor=[UIColor clearColor];
[tableViewVehiculesPossedes setDelegate:self];
[tableViewVehiculesPossedes setDataSource:self];
[self.vosvehiculeScrollView addSubview:tableViewVehiculesPossedes];
nameVehiculeLabel.text=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele"];
self.transmissionString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"transmision"];
self.carburantString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"carburant"];
self.anneeModelString=[[vehiculesPossede objectAtIndex:page] valueForKey:#"modele_annee"];
self.anneeString=[[vehiculesPossede objectAtIndex:page]valueForKey:#"annee"];
if(page==0){
NSLog(#"0");
self.transmissionString=#"ttt";
}
else NSLog(#"1");
}
The problem is that even I put [self.tableViewVehiculesPossedes release]; on the change page method, the tableView appear overlay and the text from labels are overlay. What can I do with the tableview to make it dissapear when a new page will be display?
Please help me..I spent a lot of time with this :|
If I get it correctly I think you should keep an ivar to the table you create with the alloc in loadScrollViewWithPage, and when switching page you should removing it from the view by calling :
[mytableview removeFromSuperview];
And most likely your should take care of releasing it as well as I can't see it in your code.

Remove a subview from UIScrollView

i want to remove currencyViews (tableViews) that are not more needed. i can change dynamically them, and if i had 6 currencyViews and now are 4 , those 2 (not showed) still exist. how can i remove them ?
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= numberOfCurrencyViews) return;
// replace the placeholder if necessary
CurrencyViewController *controller = [self.currencyControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[CurrencyViewController alloc] initWithPageNumber:page];
controller.delegate = self;
[self.currencyControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview)
{
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
You can remove a subview from a view using something to the extent of:
[controller.view removeFromSuperview];
[myScrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];

How do I unload a UIViewController from a UIScrollView?

I am using pretty much the same code to load my UIViewController (called LessonScrollView) into the UIScrollView. The method is called when in the scrollViewDidScroll method
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= numPages)
return;
LessonScrollView *controller = [imageViewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[LessonScrollView alloc] initWithPage:page forLesson:[self title]];
[imageViewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (controller.view.superview == nil) {
CGRect frame = CGRectMake(0, 80, 320, 285);
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
In some circumstances I have quite a few pages, which I believe, remain in memory until the UIViewController responsible for the scrollView is released (or a memory warning occurs).
My question is, how would I release the LessonScrollView object. I would call a method called, say
- (void)unloadScrollViewWithPage:(int)page
in scrollViewDidScroll and page would be the current page - 1.
Thanks for any help you can offer.
Try this:
[controller.view removeFromSuperview];//to release the controller's view
[imageViewControllers replaceOjectAtIndex:page withObject:[NSNull null]];//to release the actual controller which should lead to it being dealocated

Page Scroll View Help, different views controllers each page?

Ok, so I am trying to a pagescrollView with two views each with a different view controller so I can work on each different view. I got apple's sample code and it seems that they created a lot of different view "lazily as they said" how can this code be altered so that I can have the first page be one view controller and the second page be another view controller?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}
Thanks if anyone knows how to do this.
To load the pages with different view controllers all you have to change is this segment of code
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
to initialize each view controller to the one you want instead of the same one each time, so the code would look something like
if(page==0)
{
MyViewControllerZero *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewControllerZero alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
}
if(page==1)
{
MyViewControllerOne *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null])
{ controller = [[MyViewControllerOne alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
}
//and so on ...
This is the Correct Code!!!
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;
// replace the placeholder if necessary
if (page == 0){
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
if (page == 1){
PageOne *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PageOne alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
}
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
// add the controller's view to the scroll view
}
}
THX For the first answewr she open my mind!!!