Here is my scenario:
Main VC : Scroll View : [Multiple] MyPage : [Multiple] ObjectView : [Multple] UIButton
I have my main view controller containing a UIScrollView. The Scroll view is supposed to load multiple “MyPage” Views with pagingEnabled. I use the IB to create the “MyPage” interface (MyPage.xib) and view controller files (MyPage.h &.m). MyPage.xib file owner is set to MyPage. This “MyPage” will again load multiple ObjectView which again I created through IB with (ObjectView.xib, ObjectView.h, ObjectView.m)
Both of the MyPage & ObjectView class are subclass of UIViewController which hold some of my data properties.
The ObjectView class have several buttons and I have those button Touch Up actions connected by control drag the button to my ObjectView.h.
In my Main VC’s viewDidLoad method, I have a loop that will add all the pages:
- (void)viewDidLoad {
...
for (int i=0; i<numPages; i=i+1) {
MyPage *spg =[[MyPage alloc]i init];
...definition of spg properties...
[spg loadPage:i+1];
[scrollView addSubview:spg.view];
}
}
In the MyPage loadPage method, I have a loop that will add all the ObjectView
- (void) loadPage:(int) x {
...
for (int i=0; i<numObjects; i++) {
ObjectView *objvc = [[ObjectView alloc] init];
... defining objvc frame coordinates && other properties ...
[self.view addSubview:objvc.view];
}
}
In ObjectView, there are several buttons and I connect those buttons to TouchUp actions in IB through Ctrl-Drag the buttons to my ObjectView.h.
The App is successfully build, the scroll view did load, all pages and all objects are displayed nicely. However, when I touch the buttons the following run-time error appears:
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason:
-[TitleLayouer touchBtnOnline:]: unrecognized selector sent to instance 0x17f810′
I try to put break point on my first statement in my button action method, however that break point cannot even be reached.
Hope someone can help me with this issue.
I find out the problem and solution. The problem is due to ARC releasing the ObjectView and MyPage object when they are outside their scope (the loop?), so in run-time it cannot reference to the View Controller objects in memory anymore.
The solution is to have a NSMutableArray property in the View Controller (and remember to alloc it with init, I once forgot about it and it causes another memory error…). Inside the loop, add the view controller objects (ObjectView/MyPage) to the array so these objects will be retained in the memory.