mga/blog

ISSN 2011-0146

Múltiples views no-jerárquicas en iPhone

publicado en iphone,programación,tips por mga en October 9, 2009

(English version at the bottom of this post)

Recientemente he estado trabajando en XCode y Cocoa Touch para un proyecto que estoy haciendo para el iPhone. Objective-C (el lenguaje de programación usado) tiene sus mañas (como todo lenguaje), pero he encontrado bastante ayuda en la web, gracias especialmente a Stanford, Google y a Stackoverflow.

Creo que es pertinente devolver algo a la comunidad, en particular a los hispanoparlantes, en este caso en forma de un mini framework para desarrollo de aplicaciones con múltiples pantallas no-jerárquicas ni lineales (simplemente que uno pueda saltar de una pantalla a otra sin ningún orden en particular, cuando el UITabBarController no es una opción). En realidad es basado en este otro de Jeffrey Berthiaume, pero con la adición de NIBs (Berthiaume hace las pantallas a punta de código).

Algo importante a tener en cuenta al usar NIBs es que para que el iPhone efectivamente libere la memoria usada por NIBs con imágenes, lo mejor es cargarlas con código (pero ese es otro post). De pronto luego pongo un tip acá pero por ahora mejor visite mi pregunta al respecto de esto en Stackoverflow.

Si lo que acabo de escribir suena un poco a chino avanzado (para aquellos no familiarizados con Obj-C), en parte es porque lo es (para mi Obj-C ha sido como un camino zen… el camino del ninja o algo así). Inauguro, entonces, el tag “iphone” en mi blog con este ZIP que pueden bajar y usar libremente (y si se le tira el proyecto que está haciendo no me eche la culpa):

Descargar InfiniteViews

Para que los angloparlantes puedan llegar a este post, a continuación la versión en inglés:

English version

This is a a framework based on the work of Jeffrey Berthiaume for developing NIB-based iPhone applications with non-linear, non-hierarchichal UIViewControllers, when the UITabBarController is not an option (games with multiple NIBs come to mind). Keep in mind that, in order for Obj-C to free NIBs with UIImageViews from memory, you need to load the images via code without using imageNamed. More info regarding this can be seen in this question in Stackoverflow:

Download InfiniteViews

28 comentarios to 'Múltiples views no-jerárquicas en iPhone'

Suscríbase a los comentarios con RSS o TrackBack a 'Múltiples views no-jerárquicas en iPhone'.


  1. on October 9th, 2009 at 09:56

    This is fantastic work (and an excellent use of StackOverflow as well)!

  2. mielconejo said,

    on October 9th, 2009 at 18:00

    nice!

    : )

  3. Ali said,

    on April 6th, 2010 at 14:06

    Excellent work, mga! In wee hours of night when frustration level was reaching roof high, this code really came as Almighty’s blessing. Thanks for posting this!

  4. Vip Malixi said,

    on April 6th, 2010 at 14:44

    Good job. But when I try to use a TableViewController as the fourthcontroller — it just shows a blank tableview–the data is not shown. Anyway, if you have any ideas why the tableview is blank…

  5. mga said,

    on April 6th, 2010 at 14:52

    Each view must have its own viewcontroller (must be self-contained). You can try first doing an app with only one view (the table) and make it work. You just keep adding views as needed. Use the File > New File > Cocoa Touch Class > UIViewController subclass and check “With XIB for user interface” and “UITableViewController subclass”. You then call it in the RootViewController adding a case like: currentView = [[MyTableViewController alloc] initWithNibName:@”MyTableView” bundle:nil];

  6. Phio said,

    on April 13th, 2010 at 10:20

    Hi and thank you for the great work!

    I’m working on your example, and I’m wondering how easy could it be to add animations – let’s say push the first view down before the second appears etc.

    Any help/starting point for that??

  7. mga said,

    on April 13th, 2010 at 12:57

    hmmm… you may need modify the RootViewController to handle this animation. I guess you add both subviews and fade out one while fade in the other and remove the initial one once fade is finished

    this may be quite expensive in terms of resources depending on the nibs

  8. Phio said,

    on April 14th, 2010 at 02:35

    Actually I meant the transition among different views (from 1st to 2nd or 3rd and so on), so my first guess is to create an animation loop in the
    – (void) displayView:(int)intNewView;
    of the InfiniteViewsViewController. What do you think about that?

  9. mga said,

    on April 14th, 2010 at 12:23

    yes… you would need to modify that method

  10. mga said,

    on April 16th, 2010 at 15:23

    in fact, that might be precisely the way the UINavBarController animations work… you know, slide left-right when navigating in/out from subsections

  11. Phio said,

    on April 19th, 2010 at 07:57

    I’ll work on that, but first I’m trying to integrate facebook connect in my app. Being the creator of the whole setup, where would you suggest me to store the FBSession object? (it is the central object in facebook connect, and I want to be able to access it from every view).

  12. mga said,

    on April 19th, 2010 at 11:25

    I guess make a class for FB session management and call it from every view that needs it

  13. Matt Mc. said,

    on April 25th, 2010 at 17:01

    Hi mga.
    Thanks for this awesome example! Between your work and Jeffrey’s example this is really helping me understand more and more. One update I am trying to make to your example is for the UISegmentedControl to change an uneditable UITextView as opposed to a UIImageView. I have made what I thought would be appropriate changes in FirstViewController.h and .m as well as Interface Builder. While I don’t run into any errors, the application will not load properly(opens and closes quickly in the simulator). If you have a moment, could you make some suggestions of things I might be missing. Any example code would be tremendously helpful. Thanks again for sharing this!

  14. mga said,

    on April 25th, 2010 at 18:03

    the segmented control does not change a uiimageview but instantiates different uiviewcontrollers depending on the segment clicked. you should use the debugger and console to see why the application is crashing.

  15. ROHITH said,

    on April 26th, 2010 at 15:12

    Re:
    App’s base on Multiple view controllers, and Method for Switching between them(1 to 2 to 3 to 4 and VIce Versa).

    I have followed your tutors ..which really help me a lot ……!!!! I want to develop Multiple View with Navigation Based application .

    Kindly , please provide a tutor for Multiple? Views using Navigation Based Application.

    What I mean is suppose if we take 4 Views.(Moving from 1 to 2 to 3 to 4 and VICE VERSA ,using PREVIOUS and FORWARD BUTTON within these views ).
    In 1st View,data will be row
    (Ex:
    DEll
    HP
    SONY
    LENOVA)
    and when we click any ROW CONTENT, it must go to 2nd view(which contain some TEXTS, with 2 paragraph with some spacing between 2 paragraphs) .

    And in the same way we click on 2nd View it must go to 3rd view ( when we click ,here also TEXT will be displayed and should have a button like BACK(2 nd View) and FORWARD (4 View)BUTTON ).

    Generally ,It would be better if we display TEXT in LANDSCAPE MODE .

    I will be very thankful if u provide a tutor on it……

    WITH A WORD OF THANKS,
    ROHITH
    Email_id: rohithcse04@gmail.com

  16. mga said,

    on April 26th, 2010 at 16:01

    if you want to use UINavigationController I recommend you look in the iPhone Developer Reference for that class:

    http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html

    I also recommend you check out the Stanford iPhone Application Development course available in iTunes U and here: http://cs193p.stanford.edu

    The purpose of my approach is for specific cases where that class is not viable.

  17. alex said,

    on July 10th, 2010 at 02:29

    Hi MGA,

    Thank you for the great article!. The link to your source code seems to be down. Can you maybe email it to me at alexanderhope@mac.com. I’d love to see your NIB based version of Jeffrey’s work.

    Cheers,

    Alex

  18. alex said,

    on July 10th, 2010 at 02:34

    Hi,

    I downloaded the code. In your link you have …/blog/blog/… so i removed /blog and it worked great.

    Thank you,

    Alex

  19. mga said,

    on July 10th, 2010 at 02:42

    Link fixed. Thanks!

  20. alex said,

    on July 10th, 2010 at 03:15

    Hi Mauricio,

    Thank you for the great work! It has helped me learn so much about how views work. I have a question. Would it be possible to use 4 separate custom buttons instead of a segmented control? I’d like to use round rect buttons in interface builder to switch between the views without altering your code too much. I know that I need to make 4 IBOutlets and IBActions, but I’m sorta stuck. (and sort of a newbie). Maybe you could help me with 1 button and I can probably figure it out from there. ;)

    Cheers,

    Alex

  21. mga said,

    on July 11th, 2010 at 00:11

    well the segmented control is similar to the button in terms of IB but the difference is that you associate a differente event (click instead of change)

  22. alex said,

    on July 12th, 2010 at 14:08

    thanks

  23. Doug said,

    on July 28th, 2010 at 03:56

    Awsome tutorial,

    However i can´t get the views to rotate correctly.

    Each of them has the autorotate option enabled, and they autorotate fine if i switch to them while in portrait.
    If i keep the device in portrait everything works fine when switching between the other views, they display just like they should.

    Tho if i rotate a view and switch view while in landscape mode, the new view comes up sorta offset from where it should be, it comes up rotated to landscape but offset.

    I have all the views struts and springs turned on and autoresize on.

    Prolly some setting in IB on the window or the rootviewcontroller.xib but it seems like i´ve tried allmost everything and cant get it to work.

  24. mga said,

    on July 28th, 2010 at 09:37

    @Doug I wouldn’t know about your problem exactly. You could try googling for info about landscape mode in the iPhone and then connect that to InfiniteViews itself.

  25. tmk said,

    on August 27th, 2010 at 00:24

    Hi…i need to do a quiz app for an ipad…the first view is a login screen and once the credentials are verified, the second page is an instruction page with a start button…once the start button is clicked, it should display a page with a navigation controller with prev and next buttons to display the questions and options to the user…finally one last view to show the resuts…how do i do this?

  26. mga said,

    on August 27th, 2010 at 00:31

    @tmk there’s a lot of info out there, but it all comes down to you doing the hard work to put it together

  27. Matt said,

    on August 27th, 2010 at 19:13

    Thank you for the great article!. The link to your source code seems to be down. Could you perhaps email it to me at mnifield@gmail.com.

    Thanks,

    Matt

  28. mga said,

    on August 27th, 2010 at 19:20

    fixed! thanks for pointing it out

Opine